class Hash

Hash helpers

Public Instance Methods

clone() click to toggle source
# File lib/doing/hash.rb, line 37
def clone
  Marshal.load(Marshal.dump(self))
end
deep_freeze() click to toggle source

Freeze all values in a hash

@return Hash with all values frozen

# File lib/doing/hash.rb, line 11
def deep_freeze
  chilled = {}
  each do |k, v|
    chilled[k] = v.is_a?(Hash) ? v.deep_freeze : v.freeze
  end

  chilled.freeze
end
deep_freeze!() click to toggle source
# File lib/doing/hash.rb, line 20
def deep_freeze!
  replace deep_thaw.deep_freeze
end
deep_set(path, value) click to toggle source

Set a nested hash value using an array

@example {}.deep_set(['one', 'two'], 'value') # => { 'one' => { 'two' => 'value' } }

@param path [Array] key path @param value The value

# File lib/doing/hash.rb, line 62
def deep_set(path, value)
  if path.count == 1
    unless value.nil? || value =~ /^ *$/
      self[path[0]] = value
    else
      delete(path[0])
    end
  else
    if value
      self.default_proc = ->(h, k) { h[k] = Hash.new(&h.default_proc) }
      dig(*path[0..-2])[path.fetch(-1)] = value
    else
      return self unless dig(*path)

      dig(*path[0..-2]).delete(path.fetch(-1))
      path.pop
      cleaned = self
      path.each do |key|
        if cleaned[key].empty?
          cleaned.delete(key)
          break
        end
        cleaned = cleaned[key]
      end
      empty? ? nil : self
    end
  end
end
deep_thaw() click to toggle source
# File lib/doing/hash.rb, line 24
def deep_thaw
  chilled = {}
  each do |k, v|
    chilled[k] = v.is_a?(Hash) ? v.deep_thaw : v.dup
  end

  chilled.dup
end
deep_thaw!() click to toggle source
# File lib/doing/hash.rb, line 33
def deep_thaw!
  replace deep_thaw
end
stringify_keys() click to toggle source

Turn all keys into string

Return a copy of the hash where all its keys are strings

# File lib/doing/hash.rb, line 44
def stringify_keys
  each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v }
end
symbolize_keys() click to toggle source

Turn all keys into symbols

# File lib/doing/hash.rb, line 49
def symbolize_keys
  each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v }
end