# Map or collect with multidimension arrays

**URL:** <https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900>\
**Category:** ruby-talk\
**Created:** [12 August 2006 14:32 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900 "2006-08-12T14:32:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://rubytalk.org/u/Dark_Ambient)\
**Post date:** [12 August 2006 14:32 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/1 "2006-08-12T14:32:02Z")

</div>

What would be the proper call to a multi array either collect or map ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2006 14:39 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/2 "2006-08-12T14:39:22Z")

</div>

Hi --

> **···**
>
> On Sat, 12 Aug 2006, Dark Ambient wrote:
> 
> > What would be the proper call to a multi array either collect or map ?
> 
> There's no single proper thing to do; it depends entirely on the task  
> at hand.
> 
> David
> 
> --  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> &nbsp;&nbsp;&nbsp;----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://rubytalk.org/u/Dark_Ambient)\
**Post date:** [12 August 2006 14:47 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/3 "2006-08-12T14:47:03Z")

</div>

David - I'm trying to replicate some of the things in your r4r, but  
not using AR just some arrays and or hashes. Maybe it's the wrong  
approach.  
As an example

def publishers  
&nbsp;&nbsp;editions.map {|e| e.publisher}.uniq  
end

Stuart

> **···**
>
> On 8/12/06, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Sat, 12 Aug 2006, Dark Ambient wrote:
> > 
> > \> What would be the proper call to a multi array either collect or map ?
> > 
> > There's no single proper thing to do; it depends entirely on the task  
> > at hand.
> > 
> > David
> > 
> > --  
> > [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> > &nbsp;&nbsp;&nbsp;----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> > [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> > [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> > [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [12 August 2006 14:50 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/4 "2006-08-12T14:50:06Z")

</div>

dblack@wobblini.net wrote:

> Hi --
> 
> > What would be the proper call to a multi array either collect or map  
> > ?
> 
> There's no single proper thing to do; it depends entirely on the task  
> at hand.

DA, note also that map and collect are synonyms. It doesn't matter which of the two you use. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Sat, 12 Aug 2006, Dark Ambient wrote:

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://rubytalk.org/u/Joey)\
**Post date:** [12 August 2006 14:54 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/5 "2006-08-12T14:54:11Z")

</div>

I dont see anything wrong with that statement.

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

> **···**
>
> On 8/12/06, Dark Ambient \<sambient@gmail.com\> wrote:
> 
> > David - I'm trying to replicate some of the things in your r4r, but  
> > not using AR just some arrays and or hashes. Maybe it's the wrong  
> > approach.  
> > As an example
> > 
> > def publishers  
> > &nbsp;&nbsp;editions.map {|e| e.publisher}.uniq  
> > end
> > 
> > Stuart  
> > On 8/12/06, dblack@wobblini.net \<dblack@wobblini.net\> wrote:  
> > \> Hi --  
> > \>  
> > \> On Sat, 12 Aug 2006, Dark Ambient wrote:  
> > \>  
> > \> \> What would be the proper call to a multi array either collect or map ?  
> > \>  
> > \> There's no single proper thing to do; it depends entirely on the task  
> > \> at hand.  
> > \>  
> > \> David  
> > \>  
> > \> --  
> > \> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> > \> ----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> > \> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> > \> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> > \> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.  
> > \>

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://rubytalk.org/u/Dark_Ambient)\
**Post date:** [12 August 2006 15:08 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/6 "2006-08-12T15:08:15Z")

</div>

I know they are interchangeable  
But I can't make it work.

For instance -  
x = [["john", "doe"],["mary", "jane"],["jim","richards"],['sue","scott"]]  
y = x.map {|i| i + "name" }  
p y

It's returning nil on me ???

Stuart

> **···**
>
> On 8/12/06, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > dblack@wobblini.net wrote:  
> > \> Hi --  
> > \>  
> > \> On Sat, 12 Aug 2006, Dark Ambient wrote:  
> > \>  
> > \>\> What would be the proper call to a multi array either collect or map  
> > \>\> ?  
> > \>  
> > \> There's no single proper thing to do; it depends entirely on the task  
> > \> at hand.
> > 
> > DA, note also that map and collect are synonyms. It doesn't matter which of  
> > the two you use. 🙂
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [12 August 2006 16:35 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/7 "2006-08-12T16:35:08Z")

</div>

Dark Ambient wrote:

> def publishers  
> editions.map {|e| e.publisher}.uniq  
> end

Regarding your question of when to use #map and when to use #collect, I think this situation calls for #collect: you're collecting the attributes of objects. E.g.

&nbsp;&nbsp;&nbsp;people.collect{|person| person.name }

I use #map when I have more of a "method call" inside the block:

&nbsp;&nbsp;&nbsp;(1..10).map{|i| foo(i) }

That's at least the convention I follow.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2006 15:22 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/8 "2006-08-12T15:22:14Z")

</div>

Hi --

> **···**
>
> On Sun, 13 Aug 2006, Dark Ambient wrote:
> 
> > On 8/12/06, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> > 
> > > dblack@wobblini.net wrote:  
> > > \> Hi --  
> > > \>  
> > > \> On Sat, 12 Aug 2006, Dark Ambient wrote:  
> > > \>  
> > > \>\> What would be the proper call to a multi array either collect or map  
> > > \>\> ?  
> > > \>  
> > > \> There's no single proper thing to do; it depends entirely on the task  
> > > \> at hand.
> > > 
> > > DA, note also that map and collect are synonyms. It doesn't matter which of  
> > > the two you use. 🙂
> > 
> > I know they are interchangeable  
> > But I can't make it work.
> > 
> > For instance -  
> > x = [["john", "doe"],["mary", "jane"],["jim","richards"],['sue","scott"]]  
> > y = x.map {|i| i + "name" }  
> > p y
> > 
> > It's returning nil on me ???
> 
> That should give you an error, because you're trying to add a string  
> to an array. Try:
> 
> &nbsp;&nbsp;&nbsp;y = x.map {|i| i + ["name"] }
> 
> David
> 
> --  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> &nbsp;&nbsp;&nbsp;----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [12 August 2006 17:42 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/9 "2006-08-12T17:42:10Z")

</div>

Try

x = [["john", "doe"],["mary", "jane"],["jim","richards"],["sue","scott"]] # \<= "sue" not 'sue"  
y = x.map {|i| i \<\< "name" }  
p y

Regards, Morton

> **···**
>
> On Aug 12, 2006, at 11:08 AM, Dark Ambient wrote:
> 
> > I know they are interchangeable  
> > But I can't make it work.
> > 
> > For instance -  
> > x = [["john", "doe"],["mary", "jane"],["jim","richards"],['sue","scott"]]  
> > y = x.map {|i| i + "name" }  
> > p y
> > 
> > It's returning nil on me ???
> > 
> > Stuart

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2006 18:11 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/10 "2006-08-12T18:11:11Z")

</div>

Hi --

> **···**
>
> On Sun, 13 Aug 2006, Morton Goldberg wrote:
> 
> > On Aug 12, 2006, at 11:08 AM, Dark Ambient wrote:
> > 
> > > I know they are interchangeable  
> > > But I can't make it work.
> > > 
> > > For instance -  
> > > x = [["john", "doe"],["mary", "jane"],["jim","richards"],['sue","scott"]]  
> > > y = x.map {|i| i + "name" }  
> > > p y
> > > 
> > > It's returning nil on me ???
> > 
> > Try
> > 
> > x = [["john", "doe"],["mary", "jane"],["jim","richards"],["sue","scott"]] # \<= "sue" not 'sue"  
> > y = x.map {|i| i \<\< "name" }  
> > p y
> 
> The disadvantage of that is that it changes the original objects  
> inside x.
> 
> David
> 
> --  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> &nbsp;&nbsp;&nbsp;----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [12 August 2006 18:28 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/11 "2006-08-12T18:28:28Z")

</div>

You're right. Shouldn't use \<\< in this case. If OP wanted x modified, then could have just written:

x = [["john", "doe"], ["mary", "jane"], ["jim","richards"], ["sue","scott"]]  
x.each {|i| i \<\< "name"}

Regards, Morton

> **···**
>
> On Aug 12, 2006, at 2:11 PM, dblack@wobblini.net wrote:
> 
> > Hi --
> > 
> > On Sun, 13 Aug 2006, Morton Goldberg wrote:
> > 
> > > On Aug 12, 2006, at 11:08 AM, Dark Ambient wrote:
> > > 
> > > > I know they are interchangeable  
> > > > But I can't make it work.  
> > > > For instance -  
> > > > x = [["john", "doe"],["mary", "jane"],["jim","richards"],['sue","scott"]]  
> > > > y = x.map {|i| i + "name" }  
> > > > p y  
> > > > It's returning nil on me ???
> > > 
> > > Try
> > > 
> > > x = [["john", "doe"],["mary", "jane"],["jim","richards"],["sue","scott"]] # \<= "sue" not 'sue"  
> > > y = x.map {|i| i \<\< "name" }  
> > > p y
> > 
> > The disadvantage of that is that it changes the original objects  
> > inside x.
> > 
> > David
> > 
> > --  
> > [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> > &nbsp;&nbsp;----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> > [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> > [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> > [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://rubytalk.org/u/Dark_Ambient)\
**Post date:** [13 August 2006 00:25 UTC](https://rubytalk.org/t/map-or-collect-with-multidimension-arrays/29900/12 "2006-08-13T00:25:59Z")

</div>

Just wanted to thank everyone for the help.  
No doubt I'll be back for some other issues 🙂

Stuart

> **···**
>
> On 8/12/06, Morton Goldberg \<m\_goldberg@ameritech.net\> wrote:
> 
> > You're right. Shouldn't use \<\< in this case. If OP wanted x  
> > modified, then could have just written:
> > 
> > x = [["john", "doe"], ["mary", "jane"], ["jim","richards"],  
> > ["sue","scott"]]  
> > x.each {|i| i \<\< "name"}
> > 
> > Regards, Morton
> > 
> > On Aug 12, 2006, at 2:11 PM, dblack@wobblini.net wrote:
> > 
> > \> Hi --  
> > \>  
> > \> On Sun, 13 Aug 2006, Morton Goldberg wrote:  
> > \>\>  
> > \>\> On Aug 12, 2006, at 11:08 AM, Dark Ambient wrote:  
> > \>\>  
> > \>\>\> I know they are interchangeable  
> > \>\>\> But I can't make it work.  
> > \>\>\> For instance -  
> > \>\>\> x = [["john", "doe"],["mary", "jane"],["jim","richards"],  
> > \>\>\> ['sue","scott"]]  
> > \>\>\> y = x.map {|i| i + "name" }  
> > \>\>\> p y  
> > \>\>\> It's returning nil on me ???  
> > \>\>  
> > \>\> Try  
> > \>\>  
> > \>\> x = [["john", "doe"],["mary", "jane"],["jim","richards"],  
> > \>\> ["sue","scott"]] # \<= "sue" not 'sue"  
> > \>\> y = x.map {|i| i \<\< "name" }  
> > \>\> p y  
> > \>  
> > \> The disadvantage of that is that it changes the original objects  
> > \> inside x.  
> > \>  
> > \> David  
> > \>  
> > \> --  
> > \> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> > \> ----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> > \> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> > \> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> > \> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.  
> > \>
