Today I Learned

hashrocket A Hashrocket project

Map with Index

Ever wanted map_with_index, like each_with_index except for map instead of each? Turns out you can, with just a 1 character change. with_index is a method on Enumerator that lets you do just that:

['a', 'b', 'c'].map.with_index do |x, index|
  [x, index]
end
#=> [["a", 0], ["b", 1], ["c", 2]]

Docs

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.