Project

General

Profile

Actions

Bug #4282

closed

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

Added by dre3k (Andrei Kulakov) almost 14 years ago. Updated over 13 years ago.

Status:
Rejected
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 # => #<Enumerator: 1..2:map>

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

Actions #1

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

Actions #2

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

Actions #3

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0