Project

General

Profile

Bug #4282

Range#map inconsistency with blocks like {...} and do...end

Added by dre3k (Andrei Kulakov) over 9 years ago. Updated over 9 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]
Backport:
[ruby-core:34504]

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 # => #

p (1..2).map do |i|
i
end.to_a.class # => #
=end

#1

Updated by shyouhei (Shyouhei Urabe) over 9 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

#2

Updated by dre3k (Andrei Kulakov) over 9 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

#3

Updated by nobu (Nobuyoshi Nakada) over 9 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

Also available in: Atom PDF