refer: ruby on rails - What does .call do? - Stack Overflow
Ruby使用call 可以调用方法或者proc
m = 12.method("+")
# => `method` gets the `+` method defined in the `Fixnum` instance
# m.class
# => Method
m.call(3) #=> 15
# `3` is passed inside the `+` method as argument
m.call(20) #=> 32
send方法也可以调用方法,在本地写一个测试:
def say_hi
puts "hihi"
end
def say_goodbye
puts "goodbye"
end
# 使用send方式调用方法
name = "say_hi"
send(name)
m = 12.method("+")
# => `method` gets the `+` method defined in the `Fixnum` instance
# m.class
# => Method
m.call(3) #=> 15
puts m.call(3)
# `3` is passed inside the `+` method as argument
m.call(20) #=> 32
puts m.call(20)
运行:ruby hi.rb
得到输出:
hihi
15
32