Mega Code Archive

 
Categories / Ruby / Array
 

Building Up a Hash Using Injection

collection = [ [1, 'one'], [2, 'two'], [3, 'three'],                [4, 'four'], [5, 'five']              ] collection.inject({}) do |hash, value|   hash[value.first] = value.last   hash end p collection # => {5=>"five", 1=>"one", 2=>"two", 3=>"three", 4=>"four"}