Oppinions on map vs collect

G

Gary Watson

I've been using collect in all my programs up till now, and I recently
discovered that the map function is equivalent. My question is, (and I
know that it really doesn't matter), which is more correct to use if
you're trying to stick to doing things ruby's way? What do people on
this newsgroup generally prefer using?

Thanks in advance for you oppinions.
 
D

Dick Davies

I prefer map, since to me that's what it does.

Smalltalk (the granddaddy of ruby) has a method called 'collect'
that does the same thing.
 
D

Daniel Schierbeck

Gary said:
I've been using collect in all my programs up till now, and I recently
discovered that the map function is equivalent. My question is, (and I
know that it really doesn't matter), which is more correct to use if
you're trying to stick to doing things ruby's way? What do people on
this newsgroup generally prefer using?

Thanks in advance for you oppinions.

I usually use #map if I want to collect the return values generated from
calling a method with each item in the enumerable as an argument:

results = arr.map { |item| process(item) }

I use #collect when I want to collect attributes from each item in the
enumerable:

names = contacts.collect { |contact| contact.name }

This is just what seems logical to me; I'm not aware of any real
convention on the matter, though it may be a good idea.


Cheers,
Daniel
 
D

Daniel Schierbeck

Daniel said:
I usually use #map if I want to collect the return values generated from
calling a method with each item in the enumerable as an argument:

results = arr.map { |item| process(item) }

I use #collect when I want to collect attributes from each item in the
enumerable:

names = contacts.collect { |contact| contact.name }

This is just what seems logical to me; I'm not aware of any real
convention on the matter, though it may be a good idea.


Cheers,
Daniel

Heh, I could've written that a lot clearer, but I'm way too wasted to do so.
 
M

Matt Mower

I usually use #map if I want to collect the return values generated from
calling a method with each item in the enumerable as an argument:

results =3D arr.map { |item| process(item) }

I use #collect when I want to collect attributes from each item in the
enumerable:

names =3D contacts.collect { |contact| contact.name }

I like that convention. Assuming that it's too late for Matz to
re-purpose either function name, I'll start using that myself.

Thanks,

Matt
 
D

Daniel Schierbeck

Matt said:
I like that convention. Assuming that it's too late for Matz to
re-purpose either function name, I'll start using that myself.

Thanks,

Matt

Muahahaha, my evil plan to take over the world has begun! Soon, all will
bow to my will and... wait, is this on? Crap!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top