module Rafini::Hash

Public Instance Methods

amend(...) click to toggle source
# File lib/rafini/hash.rb, line 40
def amend(...)
  dup.amend!(...)
end
amend!(*hashes) click to toggle source

hash0.amend(hash1,…)

Ammends hash with hashes. Overwrites existing keys only with first key value found in amending hashes.

{a:'A',b:'B'}.amend({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {a:'A',b:'X'}
# File lib/rafini/hash.rb, line 34
def amend!(*hashes)
  each_key do |key|
    value=hashes.find{_1.key? key}&.fetch(key) and self[key]=value
  end
  self
end
supplement(...) click to toggle source
# File lib/rafini/hash.rb, line 24
def supplement(...)
  dup.supplement!(...)
end
supplement!(*hashes) click to toggle source

hash0.supplement!(hash1,…) #=> hash

Supplements hash with hashes. Adds missing elements only.

{a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'})
#=> {a:'A',b:'B',c:'C',d:'D'}
# File lib/rafini/hash.rb, line 16
def supplement!(*hashes)
  hashes.each do |hash|
    hash.each do |key, value|
      self[key] = value unless key?(key)
    end
  end
  self
end
to_struct(&) click to toggle source

struct = hash.to_struct Why not?

# File lib/rafini/hash.rb, line 6
def to_struct(&)
  Struct.new(*keys, &).new(*values)
end