ruby语法课2

class Hello
  def initialize(_hello)
    @hello=_hello
  end

  def say_hello
    puts @hello
  end

end

Hello.new("hello hahahaha").say_hello
class SHello < Hello
  def say_word(word="helloooooooo")
    puts word;
  end
end

SHello.new("hello ffffffffffuck").say_hello
SHello.new("hello ffffffffffuck").say_word
SHello.new("hello ffffffffffuck").say_word("aaaaaaaaaaaaa")
puts "#################################################################"
class People
  def people(* name)
    puts name.class
    for i in name
      puts i.class
    end
  end

  def people_name(name1,name2,name3)
    puts "#{name1} #{name2} #{name3}"
  end

  def people_id(_h)
    puts _h[:a]
  end
end

People.new.people(1,2,"a",People.new)
array=[1,2,"a"]
People.new.people_name(*array)
People.new.people_id(:a=>"aaaaa",:b=>"bbbbbb")
puts "#################################################################"
class Jisuan
  private
  def get_num_op(str)
    array=nil
    for i in ["+","-","*","/"]
      if str.to_s.index(i)!=nil
        array=Array.new
      array[0]=i
      array+=str.to_s.split(i)
      array[1]=array[1].strip.to_i
      array[2]=array[2].strip.to_i
      end
    end
    puts array==nil ?"nil":"#{array}"
    return array
  end

  public

  def jisuan(str)
    array=get_num_op(str)
    if(array[0]=="+")
    return array[1]+array[2]
    end
    if(array[0]=="-")
    return array[1]-array[2]
    end
    if(array[0]=="*")
    return array[1]*array[2]
    end
    if(array[0]=="/")
    return array[1]/array[2]
    end
  end

end

puts Jisuan.new.jisuan("1*3")
puts "#################################################################"
class TestClass
  def say(s)
    puts s
  end

  private :say
end

begin
  TestClass.new.say
rescue Exception=>e
  puts "#{e.backtrace}"
  puts "#{$!.to_s}"
end

puts "#################################################################"
class Student
  def getName
    @name
  end

  def setName(name)
    @name=name
    return self
  end

  attr_accessor :age

end

puts Student.new.setName("ffffffffuck").getName
s=Student.new
s.age=10
puts s.age

def s.say_hello(_s)
  puts _s
end

s.say_hello("hello word")

begin
  Student.new.say_hello("hello word")
rescue Exception=>e
  puts "#{e.backtrace}"
  puts "#{$!.to_s}"
end

puts "#################################################################"

hello=Proc.new{
|name,word|
  puts "do proc"
  "#{name} is #{word}"
}
puts hello.call("yuyong","a programmer")

def say_hello(h,w)
  puts "#{h.call("yuyong","a programmer")}, #{w}"
end

say_hello(hello,"hello welcome")

hello_1 =Proc.new do |name,word|
  puts "do proc"
  "#{name} is #{word}"
end

say_hello(hello_1,"hello ffffuck")
class SStudent
  attr_accessor :name,:id
end

stu=SStudent.new do |c|
  c.name="ffffuck"
  c.id=1001
end

puts "#{stu.name}==>#{stu.id}"

puts "#################################################################"

#yield作为参数

def say
  name="fffffffff"
  yield name
end

say do |n|
  puts "fffuck,"+n
end

#yield作为返回值

def do_say
  puts "fffffffff"+yield
end

do_say do
  word="uuuuuu"
  word+"kkkkkkkkkkkkkk"
end

运行结果

hello hahahaha
hello ffffffffffuck
helloooooooo
aaaaaaaaaaaaa
#################################################################
Array
Fixnum
Fixnum
String
People
1 2 a
aaaaa
#################################################################
["*", 1, 3]
3
#################################################################
["test_2.rb:93:in `<main>'"]
private method `say' called for #<TestClass:0x000000011f3280>
#################################################################
ffffffffuck
10
hello word
["test_2.rb:126:in `<main>'"]
undefined method `say_hello' for #<Student:0x000000011f1d68>
#################################################################
do proc
yuyong is a programmer
do proc
yuyong is a programmer, hello welcome
do proc
yuyong is a programmer, hello ffffuck
==>
#################################################################
fffuck,fffffffff
fffffffffuuuuuukkkkkkkkkkkkkk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值