can anybody explain why the following code aborts with an error
irb(main):186:0> print ((1..10).collect do |x| x**2 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 `<main>'
whereas following code with the same functionality works as expected ?
irb(main):187:0> print ((1..10).collect { |x| x**2 })
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]=> nil
I did believed curly-braces "{ }" can substitute "do end" arbitrarily at block
definition.
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 it behaves different.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…