Project

General

Profile

Bug #14935

Inconsitent behaviour with puts and enumerator with different block format.

Added by puneet.sutar@gmail.com (Puneet Sutar) 12 months ago. Updated 12 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
[ruby-core:88079]

Description

Case: 1

➜  ~ ruby
puts [1,2,3].map do |i|
  puts i
end
#<Enumerator:0x007ffe930b76d0>
➜  ~
# block does get evaluated.

Case 2:

➜  ~ ruby
puts [1, 2, 3].map { |i| puts i }
1
2
3



➜  ~
# Block gets evaluated. 

My question is Shouldn't both cases give consistent output.

History

Updated by shan (Shannon Skipper) 12 months ago

#puts returns nil, so when you're mapping you end up with [nil, nil, nil], which prints with #puts as three blank lines. If you use p instead of puts inside the block, the block will return i instead of nil and you'll see the same output.

Updated by lucasbuchala (Lucas Buchala) 12 months ago

I think this is because do ... end blocks has lower precedence than {...} blocks. As documented in [1]. So, probably, not a bug.

method1 method2 { ... }     # method2's block
method1 method2 do ... end  # method1's block

[1] http://ruby-doc.org/core-2.5.1/doc/syntax/calling_methods_rdoc.html#label-Block+Argument

#3

Updated by nobu (Nobuyoshi Nakada) 12 months ago

  • Status changed from Open to Rejected

Also available in: Atom PDF