Nov 14, 2008

Difference between "do end" and "{ }" in Ruby

"{ }" has a stronger connection than "do end".


def foobar a, b
if block_given?
yield a, b
else
puts a * b
end
end



foobar is called with block.


foobar 2, 3 do |a, b|
puts a + b #=> 5
end



b is called with block.


def b
yield
end

foobar 2, b { 3 } #=> 6

No comments: