refer: ruby on rails - What does .call do? - Stack Overflow
Ruby使用call 可以調(diào)用方法或者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方法也可以調(diào)用方法,在本地寫一個測試:
def say_hi
? puts "hihi"
end
def say_goodbye
? puts "goodbye"
end
# 使用send方式調(diào)用方法
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)
運(yùn)行:ruby hi.rb
得到輸出:文章來源:http://www.zghlxwxcb.cn/news/detail-639238.html
hihi
15
32
文章來源地址http://www.zghlxwxcb.cn/news/detail-639238.html
到了這里,關(guān)于ruby send call 的簡單使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!