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.denhaven2.com/