I notice that I need to put parenthesis around arguments I pass to a Ruby method before the block. Wat is the reason for that? Boiling down to the smallest example to illustrate what I mean:
def test a
yield 100
end
test 50 { |x| x*x + 2 } # Gives an error in irb!
test(50) { |x| x*x + 2 } # Gives no error!
I didn't understand why Ruby complains about the first case. The error is along the lines of:
SyntaxError: compile error
(irb):18: syntax error, unexpected '{', expecting $end
This is related to the fact that {
...}
sometimes delimits a block and sometimes a Hash literal. Disambiguating those two uses has some odd side-effects.
If you use do
...end
, it works without the parentheses:
test 50 do |x| x*x + 2 end