lib/sixarm_ruby_ramp/enumerable.rb
# Convert an enumerable to a hash and merge values.
#
# @return [Hash<Object,Object>] a hash of the enumerable's items
#
# @example
#   array=[[:a, :b],[:c, :d],[:e, :f]]
#   array.to_h => {:a=>:b, :c=>:d, :e=>:f}
#
# If a key occurs more than once, then this will automatically
# merge the values to an array of the keys' values.
#
# @example
#   array=[[:a,:b],[:a,:c],[:a,:d]]
#   array.to_h => {:a=>[:b, :c, :d]}
#
Convert an enumerable to a hash and merge values.
Convert an enumerable to a hash and merge values .
def to_h_merge
    hash={}
    seen=Set.new
    each{|key,val|
      if hash.key? key
        if seen.include? key
          hash[key] << val
        else
          hash[key]=[hash[key]]
          hash[key] << val
          seen << key
        end
      else
        hash[key]=val
      end
    }
    return hash
  end
def to_h_merge hash = { } seen = Set . new each { | key , val | if hash . key? key if seen . include? key hash [ key ] << val else hash [ key ] = [ hash [ key ] ] hash [ key ] << val seen << key end else hash [ key ] = val end } return hash end
Convert an enumerable to a hash and merge values.
Convert an enumerable to a hash and merge values .
a hash of the enumerable's items
a hash of the enumerable ' s items
Hash<Object,Object>
example
array=[[:a, :b],[:c, :d],[:e, :f]] array.to_h => {:a=>:b, :c=>:d, :e=>:f} If a key occurs more than once, then this will automatically merge the values to an array of the keys' values.
array = [[ : a : b ] [ : c : d ] [ : e : f ]] array . to_h = > { : a = > : b : c = > : d : e = > : f } If a key occurs more than once then this will automatically merge the values to an array of the keys ' values .
example
None