Reputation: 47146
I have functions with the definition def test(¶m)
and def test(:¶m)
. What is the difference between both?
Upvotes: 0
Views: 244
Reputation: 83680
def test(&block) ...
means that our method accepts a 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
While def test(:¶m)
will throw an error.
Also you can call something like method(&:operator)
:
[1,2,3].inject(&:+)
#=> 6
That is the same as
[1,2,3].inject{|sum, i| sum+i }
Upvotes: 4
Reputation: 370455
The difference is that def test(:¶m)
causes a syntax error and def test(¶m)
does not.
Upvotes: 6