Sha256: 4bdc0d3b6d928160d1b84e0c520b83a7eb94c80fb406d89c27e7ac02c019853b

Contents?: true

Size: 742 Bytes

Versions: 14

Compression:

Stored size: 742 Bytes

Contents

# frozen_string_literal: true

module Doing
  # Hash helpers
  class ::Hash
    ##
    ## Freeze all values in a hash
    ##
    ## @return     { description_of_the_return_value }
    ##
    def deep_freeze
      map { |k, v| v.is_a?(Hash) ? v.deep_freeze : v.freeze }.freeze
    end

    def deep_freeze!
      replace deep_freeze
    end

    # Turn all keys into string
    #
    # Return a copy of the hash where all its keys are 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 symbols
    def symbolize_keys
      each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
doing-2.0.25 lib/doing/hash.rb
doing-2.0.24 lib/doing/hash.rb
doing-2.0.23 lib/doing/hash.rb
doing-2.0.22 lib/doing/hash.rb
doing-2.0.21 lib/doing/hash.rb
doing-2.0.20 lib/doing/hash.rb
doing-2.0.19 lib/doing/hash.rb
doing-2.0.18 lib/doing/hash.rb
doing-2.0.17 lib/doing/hash.rb
doing-2.0.16 lib/doing/hash.rb
doing-2.0.15 lib/doing/hash.rb
doing-2.0.13 lib/doing/hash.rb
doing-2.0.11 lib/doing/hash.rb
doing-2.0.10 lib/doing/hash.rb