Array#assoc_to_h

This is one of those methods that really should be in the Ruby core library already.

Description

Array#assoc_to_h transforms an associative array to a hash map.

Example

x = { :foo => "bar" }
y = x.map { |k, v| [k, v * 2] }

y            # => [ [ :foo,   "barbar" ] ]
y.assoc_to_h # => {   :foo => "barbar"   }

Code

class Array
  def assoc_to_h
    result = {}

    each do |key, value|
      result[key] = value
    end

    return result
  end
end