From: William James Date: 2008-03-06T01:54:55+09:00 Subject: Re: collect with index? On Mar 5, 5:55 am, Tim Conner 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"]