simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"
how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}
but I can't find a 'collect_with_index' in the documentation. How would
I do this?
Thanks
simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"
how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}
>> require "enumerator"
=> true
>> ary = ["cat", "dog", "pig"]
=> ["cat", "dog", "pig"]
>> ary.enum_for(:each_with_index).collect { |animal, index|
"#{animal} number #{index + 1}"
}
=> ["cat number 1", "dog number 2", "pig number 3"]
Or in Ruby 1.9 it's simply:
>> ary = ["cat", "dog", "pig"]
=> ["cat", "dog", "pig"]
>> ary.each_with_index.collect { |animal, index|
"#{animal} number #{index + 1}"
}
=> ["cat number 1", "dog number 2", "pig number 3"]
simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"
how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}
but I can't find a 'collect_with_index' in the documentation. How would
I do this?
simple question: how can I do a collect with index?
You mean "map with index".
This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"
how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}
That should be
%w(cat dog pig)
.collect_with_index
That should be
.map_with_index
{|animal,index| animal+" number
"+index}
but I can't find a 'collect_with_index' in the documentation. How would
I do this?
irb --prompt xmp
a = %w(cat dog pig)
==>["cat", "dog", "pig"]
a.zip( (1..a.size).to_a ).map{|x,y| "#{x} #{y}" }
==>["cat 1", "dog 2", "pig 3"]
So James Gray III would be what, James Gray, Junior Junior? Or like
Great Junior? I'm pretty sure the numbering is continued practice
that was used mostly for political reasons. Royal families during
Newton's time, for example, had strange marriages, and it was good to
keep track of someone's lineage, especially if you have the same name
floating around This isn't just a western concept, either.
I agree, #map makes more sense, but the use of #collect doesn't bother
me that much. I think I would get irritated, however, if I saw both
in the same code.
I agree, #map makes more sense, but the use of #collect doesn't bother
me that much. I think I would get irritated, however, if I saw both
in the same code.
In functional languages, I'm happily a mapper, but in OO languages I'm
an avid collector. I guess it's the Smalltalker in me.
I wouldn't baulk at mixing and matching according to conditions. I'd
probably #map a variable representing a Proc object and #collect a
block:
f = lambda {|x| ...}
a.map &f
a.collect {|x| ...}
If that makes me weird then I'm happy to be weird.
Weird is just another way to say "free thinker," anyway.
James Edward Gray II
···
On Mar 5, 2008, at 12:58 PM, Mark Bush wrote:
Todd Benson wrote:
I agree, #map makes more sense, but the use of #collect doesn't bother
me that much. I think I would get irritated, however, if I saw both
in the same code.
In functional languages, I'm happily a mapper, but in OO languages I'm
an avid collector. I guess it's the Smalltalker in me.
I wouldn't baulk at mixing and matching according to conditions. I'd
probably #map a variable representing a Proc object and #collect a
block:
f = lambda {|x| ...}
a.map &f
a.collect {|x| ...}
If that makes me weird then I'm happy to be weird.