# Map Or Collect Redux

**URL:** <https://www.ruby-forum.com/t/map-or-collect-redux/90861>\
**Category:** Ruby\
**Created:** [April 2, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861 "2007-04-02T15:13:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/1 "2007-04-02T15:13:31Z")

</div>

Looking in the old archives of ruby-talk I found a thread in 2005  
about using map or collect. As far as I know there is not difference  
between the two methods. I would like to know what everyone’s  
preference is?

I like using collect.

Stephen B. IV

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/2 "2007-04-02T15:17:31Z")

</div>

I think the aliases are there for a good syntactic reason. It seems  
to me that if you are just filtering out items which meet a certain  
criteria from one array to be put into a new one (say all elements  
which are even numbers) then you are collecting. If you’re actually  
performing a transformation on the elements then you are mapping one  
array space onto another.

example

arr = [1, 2, 3, 4, 5]  
arr.collect { |x| if x%2 == 0 then x end }.compact  
=\> [2, 4]

# Seems like maybe it should be .filter instead of .collect

arr = [1, 2, 3, 4, 5]  
arr.map { |x| x\*2 }  
=\> [2, 4, 6, 8, 10]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/3 "2007-04-02T15:24:31Z")

</div>

On 02/04/07, Nicholas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arr = [1, 2, 3, 4, 5]  
> arr.collect { |x| if x%2 == 0 then x end }.compact  
> =\> [2, 4]
> 
> # Seems like maybe it should be .filter instead of .collect

This is more appropriate for Array#select (or reject).

Personally I use map exclusively.

Farrel

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/4 "2007-04-02T15:26:34Z")

</div>

On 02.04.2007 15:17, Nicholas M. wrote:

> I think the aliases are there for a good syntactic reason. It seems  
> to me that if you are just filtering out items which meet a certain  
> criteria from one array to be put into a new one (say all elements  
> which are even numbers) then you are collecting.

No, you are actually _selecting_.

> If you’re actually  
> performing a transformation on the elements then you are mapping one  
> array space onto another.

#map and #collect behave identical. They are just there to make people  
coming from different programming languages feel at home with Ruby  
faster.

> example
> 
> arr = [1, 2, 3, 4, 5]  
> arr.collect { |x| if x%2 == 0 then x end }.compact  
> =\> [2, 4]
> 
> # Seems like maybe it should be .filter instead of .collect

No, use #select - much more efficient and it does exactly what you are  
trying to accomplish above.

Regards

robert

PS: for the record, I use #map as it is shorter to type and I am laz

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/5 "2007-04-02T16:02:37Z")

</div>

Enumerable#select is certainly what I was thinking of. Certainly  
having both collect and map would make it easier for developers coming  
from other languages to pick up ruby but often times I see code where  
these methods are both used. Ruby reads very nice and I prefer to use  
map just because it semantically makes more sense to me if I am  
performing some sort of transformation on the array elements. (Plus  
it is shorter to type : ) ). When I think of collecting elements I  
think of gathering or _selecting_ certain elements, collect just does  
not seem to imply that the elements will be changed in any way to me.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/6 "2007-04-02T17:13:20Z")

</div>

I use map, but then I was a Mathematics major, which may have something  
to do with it … 😉

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/7 "2007-04-02T16:44:42Z")

</div>

On 4/2/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Looking in the old archives of ruby-talk I found a thread in 2005  
> about using map or collect. As far as I know there is not difference  
> between the two methods. I would like to know what everyone’s  
> preference is?

I use map. It is shorter and for me it describes the operation better.

Ryan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/8 "2007-04-02T18:49:55Z")

</div>

What does Matz use?

Stephen B. IV

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/9 "2007-04-02T23:07:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I would like to know what everyone’s preference is?  
> I like using collect.

Mostly the non-smalltalk terms: map, find, find\_all, but reject. (map ==  
mathematics background.)

When I’m dealing with an ActiveRecord::AssociationProxy, find and  
find\_all are overridden by Rails, so I tend to use detect and select.  
I’m not sure I like that, though, vice just saying to\_a.find.

Devin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/10 "2007-04-02T23:34:51Z")

</div>

On 4/2/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Looking in the old archives of ruby-talk I found a thread in 2005  
> about using map or collect. As far as I know there is not difference  
> between the two methods. I would like to know what everyone’s  
> preference is?

I use map for the most part - the one place I occasionally use collect  
is when I’m mapping over a struct or class to pick out a few fields,  
e.g.

books.collect {|b| [b.author, b.title]}

If I were designing my own language, I’d have map be  
structure-preserving, and collect return an array, but that’s just my  
personal feeling.

martin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/map-or-collect-redux/90861/11 "2007-04-02T19:15:59Z")

</div>

On 4/2/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What does Matz use?
> 
> Stephen B. IV

WWMD?
