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
Tim C. wrote:
simple question: Â how can I do a collect with index?
In 1.8: require ‘enumerator’; foo.to_enum(:each_with_index).collect
{|x,i| }
In 1.9: foo.collect.with_index {|x,i| }
HTH,
Sebastian
2008/3/5, Tim C. removed_email_address@domain.invalid:
"+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"]
HTH,
Stefan
On 3/5/08, Tim C. removed_email_address@domain.invalid wrote:
"+index}
but I can’t find a ‘collect_with_index’ in the documentation. How would
I do this?
require ‘enumerator’
%w(cat dog pig).enum_with_index.collect {|e,i| “#{e} number #{i+1}”}
=> [“cat number 1”, “dog number 2”, “pig number 3”]
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
On Wed, Mar 5, 2008 at 9:00 PM, Sebastian H.
removed_email_address@domain.invalid wrote:
Tim C. wrote:
simple question: how can I do a collect with index?
In 1.8: require ‘enumerator’; foo.to_enum(:each_with_index).collect {|x,i| }
require ‘enumerator’
[:a, :b, :c].enum_with_index.map{|x, i| }
On Mar 5, 5:55 am, Tim C. removed_email_address@domain.invalid wrote:
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”]
On Mar 5, 2008, at 10:54 AM, William J. wrote:
On Mar 5, 5:55 am, Tim C. removed_email_address@domain.invalid wrote:
simple question: how can I do a collect with index?
You mean “map with index”.
Why? collect() is an alias for map()? Some favor one, some the other.
There was no problem with the request.
James Edward G. II
On Mar 5, 2008, at 11:19 AM, William J. wrote:
other.
Some actually favor drinking urine. That doesn’t make it right.
“map” describes what is done; “collect” does not, and is consequently
misleading.
Well it’s obvious that at least myself and the creator of Ruby disagree.
James Edward G. II
On Mar 5, 11:00 am, James G., Junior removed_email_address@domain.invalid
wrote:
On Mar 5, 2008, at 10:54 AM, William J. wrote:
On Mar 5, 5:55 am, Tim C. removed_email_address@domain.invalid wrote:
simple question: how can I do a collect with index?
You mean “map with index”.
Why? collect() is an alias for map()? Some favor one, some the other.
Some actually favor drinking urine. That doesn’t make it right.
“map” describes what is done; “collect” does not, and is consequently
misleading.
What if “pick_and_choose” were an alias for “map”? Would it make
sense
to use it? No, it wouldn’t. It doesn’t describe what is being done.
“map” is shorter and clearer. There’s every reason to use it and no
reason not to use it. Just as “junior” is preferable to
“the second”.
There was no problem with the request.
Then there’s no problem with “junior”.
On Wed, Mar 5, 2008 at 11:19 AM, William J. removed_email_address@domain.invalid
wrote:
Then there’s no problem with “junior”.
James G., Junior
So James G. III would be what, James G., 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.
Todd
Todd B. 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.
On Mar 5, 2008, at 12:58 PM, Mark B. wrote:
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.
I like weird people. 
Weird is just another way to say “free thinker,” anyway.
James Edward G. II