Issue #6155 has been updated by Marc-Andre Lafortune.

Category set to core
Target version set to 2.0.0

Hi,

Shugo Maeda wrote:
> 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.

I understand your point of view. This behavior was clearly intended in Matz's original commit, though (r25456). Changing this could also introduce some compatibility issues.

I feel it might be best to keep the current behavior, but Matz will have the final word on this.


----------------------------------------
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-24661

Author: Dan Kubb
Status: Assigned
Priority: Normal
Assignee: Shugo Maeda
Category: core
Target version: 2.0.0
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/