What would be the proper call to a multi array either collect or map ?
TIA
Stuart
What would be the proper call to a multi array either collect or map ?
TIA
Stuart
Hi –
On Sat, 12 Aug 2006, Dark A. 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
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
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, Dark A. wrote:
There’s no single proper thing to do; it depends entirely on the task
x = [[“john”, “doe”],[“mary”, “jane”],[“jim”,“richards”],['sue",“scott”]]
y = x.map {|i| i + “name” }
p yIt’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
[email protected] wrote:
Hi –
On Sat, 12 Aug 2006, Dark A. 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 A. 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, Morton G. wrote:
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
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
Try
x = [[“john”, “doe”],[“mary”, “jane”],[“jim”,“richards”],
[“sue”,“scott”]] # <= “sue” not 'sue"
y = x.map {|i| i << “name” }
p y
Regards, Morton
Just wanted to thank everyone for the help.
No doubt I’ll be back for some other issues
Stuart
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs