lib/webget_ramp/enumerable.rb in webget_ramp-1.7.1.7 vs lib/webget_ramp/enumerable.rb in webget_ramp-1.7.1.8
- old
+ new
@@ -173,9 +173,25 @@
def map_to_sym
map{|x| x.to_sym}
end
+ # Map each item and its index => a new output
+ #
+ # cf. Enumerable#map, Enumerable#each_with_index
+ #
+ # ==Example
+ #
+ # strings = ["a", "b", "c"]
+ # strings.map_with_index{|string,index| "#{string}#{index}"}
+ # => ["a0, "b1", "c3"]
+
+ def map_with_index
+ i=-1
+ map{|x| i+=1; yield(x,i)}
+ end
+
+
########################################################################
#
# select
#
########################################################################