Collect with index?

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

···

--
Posted via http://www.ruby-forum.com/.

Tim Conner 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

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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"]

HTH,
  Stefan

···

2008/3/5, Tim Conner <plump4651@googlemail.com>:

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"]

···

On 3/5/08, Tim Conner <plump4651@googlemail.com> wrote:

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?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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, 5:55 am, Tim Conner <plump4...@googlemail.com> wrote:

Tim Conner 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 Wed, Mar 5, 2008 at 9:00 PM, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

In 1.9: foo.collect.with_index {|x,i| }

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Thanks all

···

--
Posted via http://www.ruby-forum.com/.

Why? collect() is an alias for map()? Some favor one, some the other.

There was no problem with the request.

James Edward Gray II

···

On Mar 5, 2008, at 10:54 AM, William James wrote:

On Mar 5, 5:55 am, Tim Conner <plump4...@googlemail.com> wrote:

simple question: how can I do a collect with index?

You mean "map with index".

>> 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 Mar 5, 11:00 am, James Gray, Junior <ja...@grayproductions.net> wrote:

On Mar 5, 2008, at 10:54 AM, William James wrote:
> On Mar 5, 5:55 am, Tim Conner <plump4...@googlemail.com> wrote:

James Gray, Junior

Well it's obvious that at least myself and the creator of Ruby disagree.

James Edward Gray II

···

On Mar 5, 2008, at 11:19 AM, William James wrote:

On Mar 5, 11:00 am, James Gray, Junior <ja...@grayproductions.net> > wrote:

On Mar 5, 2008, at 10:54 AM, William James wrote:

On Mar 5, 5:55 am, Tim Conner <plump4...@googlemail.com> 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.

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 :wink: 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

···

On Wed, Mar 5, 2008 at 11:19 AM, William James <w_a_x_man@yahoo.com> wrote:

Then there's no problem with "junior".

>
> James Gray, Junior

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.

···

--
Posted via http://www.ruby-forum.com/.

I like weird people. :wink:

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.