Issue #6155 has been updated by Shugo Maeda.

Assignee set to Shugo Maeda

Hello,

Dan Kubb wrote:
> The following statement will raise "NoMethodError: undefined method `each' for 1:Fixnum":
> 
>   [1, 2, 3].lazy.flat_map { |n| n }.to_a
> 
> It appears as if Enumerable::Lazy#flat_map is calling #each on every element, regardless of whether it can work or not.
> 
> As a reference, the equivalent statement using Enumerable#flat_map works:
> 
>   [1, 2, 3].flat_map { |n| n }.to_a

I doubt that this behavior of Enumerable#flat_map is reasonable.
flat_map is designed to concatenate multiple collections into one collection, so in the above example, map should be used instead of flat_map.

[1, 2, 3].concat(4) raises a TypeError, so [1, 2, 3].flat_map { |n| n } should raise an error, I think.

----------------------------------------
Bug #6155: Enumerable::Lazy#flat_map raises an exception when an element does not respond to #each
https://bugs.ruby-lang.org/issues/6155#change-24643

Author: Dan Kubb
Status: Open
Priority: Normal
Assignee: Shugo Maeda
Category: 
Target version: 
ruby -v: ruby 2.0.0dev (2012-03-15 trunk 35028) [x86_64-darwin11.3.0]


The following statement will raise "NoMethodError: undefined method `each' for 1:Fixnum":

  [1, 2, 3].lazy.flat_map { |n| n }.to_a

It appears as if Enumerable::Lazy#flat_map is calling #each on every element, regardless of whether it can work or not.

As a reference, the equivalent statement using Enumerable#flat_map works:

  [1, 2, 3].flat_map { |n| n }.to_a


-- 
http://bugs.ruby-lang.org/