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
# => Methodm.call(3) #=> 15
# `3` is passed inside the `+` method as argument
m.call(20) #=> 32
send方法也可以调用方法,在本地写一个测试:
def say_hiputs "hihi"enddef say_goodbyeputs "goodbye"end# 使用send方式调用方法name = "say_hi"send(name)m = 12.method("+")# => `method` gets the `+` method defined in the `Fixnum` instance# m.class# => Methodm.call(3) #=> 15puts m.call(3)# `3` is passed inside the `+` method as argumentm.call(20) #=> 32puts m.call(20)
运行:ruby hi.rb
得到输出:
hihi1532