module Rafini::Hash
Public Instance Methods
amend(...)
click to toggle source
# File lib/rafini/hash.rb, line 38 def amend(...) self.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 32 def amend!(*hashes) self.each_key do |key| value=hashes.find{_1.has_key? key}&.fetch(key) and self[key]=value end self end
supplement(...)
click to toggle source
# File lib/rafini/hash.rb, line 23 def supplement(...) self.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 15 def supplement!(*hashes) hashes.each do |hash| hash.each do |key, value| self[key] = value unless self.has_key?(key) end end self end
to_struct(&blk)
click to toggle source
struct = hash.to_struct Why not?
# File lib/rafini/hash.rb, line 6 def to_struct(&blk) Struct.new(*keys, &blk).new(*values) end