Building Up a Hash Using Injection : inject « Array « Ruby






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"}

 








Related examples in the same category

1.Sum an array with inject
2.Sum of word lengths