[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

collect with index?

Tim Conner

3/5/2008 11:55:00 AM

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

12 Answers

Sebastian Hungerecker

3/5/2008 12:00:00 PM

0

Tim Conner wrote:
> simple question: =C2=A0how 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
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Stefan Lang

3/5/2008 12:06:00 PM

0

2008/3/5, Tim Conner <plump4651@googlemail.com>:
> 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

Michael Fellinger

3/5/2008 12:09:00 PM

0

On Wed, Mar 5, 2008 at 9:00 PM, Sebastian Hungerecker
<sepp2k@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| }

> In 1.9: foo.collect.with_index {|x,i| }
>
> HTH,
> Sebastian
> --
> Jabber: sepp2k@jabber.org
> ICQ: 205544826
>
>

Rick DeNatale

3/5/2008 12:09:00 PM

0

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?

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

Tim Conner

3/5/2008 12:15:00 PM

0

Thanks all
--
Posted via http://www.ruby-....

William James

3/5/2008 4:51:00 PM

0

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

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

James Gray

3/5/2008 5:01:00 PM

0

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.

There was no problem with the request.

James Edward Gray II

William James

3/5/2008 5:17:00 PM

0

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.

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

>
> James Gray, Junior

James Gray

3/5/2008 5:30:00 PM

0

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.

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

James Edward Gray II

Todd Benson

3/5/2008 5:49:00 PM

0

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

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.

Todd