map or collect with multidimension arrays

D

Dark Ambient

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

TIA
Stuart
 
D

dblack

R

Robert Klemme

Hi --



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
 
D

Dark Ambient

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


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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

Dark Ambient

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
 
D

dblack

Hi --

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:

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

Daniel Schierbeck

Dark said:
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
 
M

Morton Goldberg

Try

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

Regards, Morton
 
D

dblack

Hi --

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Morton Goldberg

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

Hi --

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

Dark Ambient

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

Stuart

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

Hi --

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top