Bug #4282
closedRange#map inconsistency with blocks like {...} and do...end
Description
=begin
I'm not sure that it's supposed to be like that:
p (1..2).map{|i|
i
}.class # => Array
p (1..2).map do |i|
i
end.class # => #<Enumerator: 1..2:map>
p (1..2).map do |i|
i
end.to_a.class # => #<Enumerator: 1..2:map>
=end
Updated by shyouhei (Shyouhei Urabe) almost 14 years ago
- Status changed from Open to Rejected
=begin
Yes it is. It has nothing to do with Range, but the p method.
p (...).meth { ... } is parsed as p( (...).meth { ... } ), while
p (...).meth do ... end is parsed as p( (...).meth ) do ... end.
When in doubt, you should explicitly put parentheses around your expression.
=end
Updated by dre3k (Andrei Kulakov) almost 14 years ago
=begin
I got that, but one thing: how exactly should I explicitly put parentheses around my expression?
Doing so:
p ((1..2).map do |i|
i
end)
yields syntax errors:
issue.rb:1: syntax error, unexpected keyword_do_block, expecting ')'
p ((1..2).map do |i|
^
issue.rb:3: syntax error, unexpected keyword_end, expecting $end
=end
Updated by nobu (Nobuyoshi Nakada) almost 14 years ago
=begin
(11/01/16 0:27), Andrei Kulakov wrote:
I got that, but one thing: how exactly should I explicitly put parentheses around my expression?
Doing so:
p ((1..2).map do |i|
i
end)yields syntax errors:
issue.rb:1: syntax error, unexpected keyword_do_block, expecting ')'
p ((1..2).map do |i|
^
issue.rb:3: syntax error, unexpected keyword_end, expecting $end
Don't put a space between the method name and the argument.
When a space is placed, parentheses are considered as argument grouping,
and arguments can't have DO block.
--
Nobu Nakada
=end