我有定义为def test(¶m)和def test(:¶m)的函数。两者之间的区别是什么?
发布于 2011-08-14 23:38:02
def test(&block) ...意味着我们的方法接受一个块:
def test(number, &block)
yield number
# same as
# block.call number
end
test(10) {|a| a+a}
#=> 20
# or
block = proc{|a| a*a}
test 10, &block
#=> 100而def test(:¶m)将抛出一个错误。
你也可以调用像method(&:operator)这样的东西
[1,2,3].inject(&:+)
#=> 6这是相同的
[1,2,3].inject{|sum, i| sum+i }发布于 2011-08-14 23:26:18
不同之处在于def test(:¶m)会导致语法错误,而def test(¶m)不会。
https://stackoverflow.com/questions/7057658
复制相似问题