Ruby’s Array and Enumerable classes add the to_h method in 2.1. The enumerable version takes a list, requires the index, effectively turning it into an array, and then Ruby parses it into a hash with to_h.

>> %w{salad rice salmon}.each_with_index.to_h
=> {"salad"=>0, "rice"=>1, "salmon"=>2}

:)

Leave a Reply

Your email address will not be published. Required fields are marked *