Forum: Ruby Oppinions on map vs collect

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0b26d242cc8af969cb83b045d679348?d=identicon&s=25 pfharlock (Guest)
on 2005-12-05 14:03
(Received via mailing list)
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.
A52b0e1c5d982f2512a03c5dbfd033d6?d=identicon&s=25 rasputnik (Guest)
on 2005-12-05 14:12
(Received via mailing list)
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.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-05 14:24
(Received via mailing list)
Gary Watson wrote:
> 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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-05 15:01
(Received via mailing list)
Daniel Schierbeck wrote:
> calling a method with each item in the enumerable as an argument:
>
>
> Cheers,
> Daniel

Heh, I could've written that a lot clearer, but I'm way too wasted to do
so.
Efbfad4ef638cfb7d1145ead4870e2d0?d=identicon&s=25 matt.mower (Guest)
on 2005-12-05 15:09
(Received via mailing list)
On 05/12/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
> 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 }
>

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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-05 15:34
(Received via mailing list)
Matt Mower wrote:
>>
>
> 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
>
> --
> Matt Mower :: http://matt.blogs.it/

Muahahaha, my evil plan to take over the world has begun! Soon, all will
bow to my will and... wait, is this on? Crap!
Dabdaf107905a3605174b18ead82d45d?d=identicon&s=25 Jason Sweat (sweatje)
on 2005-12-05 15:58
(Received via mailing list)
On 12/5/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
> Muahahaha, my evil plan to take over the world has begun! Soon, all will
> bow to my will and... wait, is this on? Crap!
>

Too late, there are already evil programmers:
http://netevil.org/wiki.php?PlansForWorldDomination


Regards,
Jason
http://blog.casey-sweat.us/
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-05 17:25
(Received via mailing list)
Jason Sweat wrote:
> Jason
> http://blog.casey-sweat.us/

God damn it! There's always someone who's thought of doing everything
before me!

But does his plan include pickles and nude photographs of Jon Steward,
huh?! I don't think so!! Muahahahahahahaha!!!

Evil Cheers,
Dr. Daniel "Gates" Schierbeck
This topic is locked and can not be replied to.