Tag Archives: enumerable

Extend enumerable to add method collect_with_index

module Enumerable def collect_with_index(i=0) collect{|elm| yield(elm, i+=1)} end alias map_with_index collect_with_indexend #Example use : ree-1.8.7-2010.01 > [‘ruby’, ‘rails’, ‘sandip’].map_with_index{ |w,i| [w, i] } #=> [[“ruby”, 1], [“rails”, 2], [“sandip”, 3]] ree-1.8.7-2010.01 > [‘ruby’, ‘rails’, ‘sandip’].collect_with_index{ |w,i| [w, i] } #=> … Continue reading

Posted in Ruby | Tagged , , | Leave a comment