Map or collect with multidimension arrays

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

TIA
Stuart

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 => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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
  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 => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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. :slight_smile:

    robert

···

On Sat, 12 Aug 2006, Dark Ambient wrote:

I dont see anything wrong with that statement.

j`ey
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
  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 => Ruby/Rails training & consultancy
> ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
> http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
> Ruby for Rails => book, Ruby for Rails
> http://www.rubycentral.org => Ruby Central, Inc.
>

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. :slight_smile:

    robert

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.

   people.collect{|person| person.name }

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

   (1..10).map{|i| foo(i) }

That's at least the convention I follow.

Cheers,
Daniel

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. :slight_smile:

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:

   y = x.map {|i| i + ["name"] }

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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

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 => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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 => Ruby/Rails training & consultancy
  ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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

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 => Ruby/Rails training & consultancy
> ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
> http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
> Ruby for Rails => book, Ruby for Rails
> http://www.rubycentral.org => Ruby Central, Inc.
>