Sha256: 66f31b24719ef1e12217de7c5ed46232c471b6a02ba381ca4cc6ca358de09eb7

Contents?: true

Size: 1.42 KB

Versions: 41

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

# Hash helpers
class ::Hash
  ##
  ## Freeze all values in a hash
  ##
  ## @return     [Hash] Hash with all values frozen
  ##
  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 a hash in place (destructive)
  ##
  def deep_freeze!
    replace deep_thaw.deep_freeze
  end

  ##
  ## Unfreeze nested hash values
  ##
  ## @return     [Hash] Hash with all values unfrozen
  ##
  def deep_thaw
    chilled = {}
    each do |k, v|
      chilled[k] = v.is_a?(Hash) ? v.deep_thaw : v.dup
    end

    chilled.dup
  end

  ##
  ## Unfreeze nested hash values in place (destructive)
  ##
  def deep_thaw!
    replace deep_thaw
  end

  # Turn all keys into string
  #
  # @return     [Hash] hash with all keys as strings
  #
  def stringify_keys
    each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v }
  end

  ##
  ## Turn all keys into strings in place (destructive)
  ##
  def stringify_keys!
    replace stringify_keys
  end

  # Turn all keys into symbols
  #
  # @return     [Hash] hash with all keys as symbols
  #
  def symbolize_keys
    each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v }
  end

  ##
  ## Turn all keys into symbols in place (destructive)
  ##
  def symbolize_keys!
    replace symbolize_keys
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
howzit-2.1.18 lib/howzit/hash.rb
howzit-2.1.16 lib/howzit/hash.rb
howzit-2.1.15 lib/howzit/hash.rb
howzit-2.1.14 lib/howzit/hash.rb
howzit-2.1.13 lib/howzit/hash.rb
howzit-2.1.12 lib/howzit/hash.rb
howzit-2.1.10 lib/howzit/hash.rb
howzit-2.1.9 lib/howzit/hash.rb
howzit-2.1.8 lib/howzit/hash.rb
howzit-2.1.7 lib/howzit/hash.rb
howzit-2.1.6 lib/howzit/hash.rb
howzit-2.1.5 lib/howzit/hash.rb
howzit-2.1.4 lib/howzit/hash.rb
howzit-2.1.3 lib/howzit/hash.rb
howzit-2.1.2 lib/howzit/hash.rb
howzit-2.1.1 lib/howzit/hash.rb
howzit-2.1.0 lib/howzit/hash.rb
howzit-2.0.34 lib/howzit/hash.rb
howzit-2.0.33 lib/howzit/hash.rb
howzit-2.0.32 lib/howzit/hash.rb