[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

map or collect with multidimension arrays

Dark Ambient

8/12/2006 2:32:00 PM

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

TIA
Stuart

10 Answers

dblack

8/12/2006 2:39:00 PM

0

Robert Klemme

8/12/2006 2:47:00 PM

0

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

robert

Dark Ambient

8/12/2006 2:47:00 PM

0

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

Dark Ambient

8/12/2006 3:08:00 PM

0

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. :-)
>
> robert
>
>
>

dblack

8/12/2006 3:22:00 PM

0

Daniel Schierbeck

8/12/2006 4:30:00 PM

0

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

Morton Goldberg

8/12/2006 5:42:00 PM

0

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


dblack

8/12/2006 6:11:00 PM

0

Morton Goldberg

8/12/2006 6:28:00 PM

0

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


Dark Ambient

8/13/2006 12:26:00 AM

0

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