Block as method argument (Ruby 1.9)

Hi,

can anybody explain while the following code aborts with an error

irb(main):186:0> print ((1…10).collect do |x| x2 end)
SyntaxError: (irb):186: syntax error, unexpected keyword_do_block,
expecting ‘)’
print ((1…10).collect do |x| x
2 end)
^
(irb):186: syntax error, unexpected keyword_end, expecting $end
print ((1…10).collect do |x| x**2 end)
^
from /usr/bin/irb:12:in `’

whereas following code works as expectd ?

irb(main):187:0> print ((1…10).collect { |x| x**2 })
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]=> nil

I did thought curly-braces “{ }” are equivalent to “do end” when block
is defined.

I know I can “fix” the first call by omitting a space between print
method and the first parenthesis, but I’m interested in an explanation
why .

Thx in an advance.

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs