Inconsistency - map/collect not returning enumerator

B

Brian Candler

This seems a bit of an anomoly:
x = [1,2,3].map
x.each { |y| y*2 }
=> [1, 2, 3] # I was expecting [2, 4, 6]

It seems that Array#map and Enumerable#map without a block return just
an Array. However most other Enumerable methods return an Enumerator,
even #each (which is pretty useless, since the Enumerator maps #each to
#each)
=> "ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]"
[1,2,3].each
=> # said:
[1,2,3].map => [1, 2, 3]
[1,2,3].collect
=> [1, 2, 3]
=> [1]

Anybody got a good explanation for this?
 
G

Gregory Brown

This seems a bit of an anomoly:
x =3D [1,2,3].map
x.each { |y| y*2 }
=3D> [1, 2, 3] =A0 =A0 =A0 =A0 =A0 =A0# I was expecting [2, 4, 6]

It seems that Array#map and Enumerable#map without a block return just
an Array. However most other Enumerable methods return an Enumerator,
even #each (which is pretty useless, since the Enumerator maps #each to
#each)
=3D> "ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]"
Anybody got a good explanation for this?

Yes. You're using Ruby 1.8.7, which is, in and of itself, an anomaly :)

But this may be to preserve compatibility with Ruby 1.8.6:
=3D> "1.8.6"

# NOT OK TO CHANGE BEHAVIOR
[1,2,3].map
=3D> [1, 2, 3]

# OK TO ADD BEHAVIOR
LocalJumpError: no block given
from (irb):3:in `select'
from (irb):3
 
B

Brian Candler

Gregory said:
But this may be to preserve compatibility with Ruby 1.8.6:

Ah, that's a good enough reason.

'ri Array#map' from 1.8.6 doesn't mention the possibility that it could
be used without a block, which led me off the scent.

-------------------------------------------------------------- Array#map
array.collect {|item| block } -> an_array
array.map {|item| block } -> an_array
------------------------------------------------------------------------
Invokes _block_ once for each element of _self_. Creates a new
array containing the values returned by the block. See also
+Enumerable#collect+.

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top