Class: HashHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



2
3
4
# File 'lib/hash_helper.rb', line 2

def self.new
  @instance ||= super
end

Instance Method Details

#deep_merge(hash, other_hash) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/hash_helper.rb', line 11

def deep_merge(hash, other_hash)
  hash.merge(other_hash) do |key, oldval, newval|
    oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
    newval = newval.to_hash if newval.respond_to?(:to_hash)
    oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? deep_merge(oldval, newval) : newval
  end
end

#hasherize(array, &block) ⇒ Object



6
7
8
9
# File 'lib/hash_helper.rb', line 6

def hasherize(array, &block)
  block ||= ->(e) { e }
  Hash[*array.map{|e| [e, block.call(e)] }.flatten]
end