module Hashie::Extensions::StringifyKeys::ClassMethods

Public Instance Methods

stringify_keys(hash) click to toggle source

Return a copy of hash with all keys converted to strings. @param [::Hash] hash

# File lib/hashie/extensions/stringify_keys.rb, line 57
def stringify_keys(hash)
  copy = hash.dup
  copy.extend(Hashie::Extensions::StringifyKeys) unless copy.respond_to?(:stringify_keys!)
  copy.tap do |new_hash|
    stringify_keys!(new_hash)
  end
end
stringify_keys!(hash) click to toggle source

Convert all keys in the hash to strings.

@param [::Hash] hash @example

test = {:abc => 'def'}
test.stringify_keys!
test # => {'abc' => 'def'}
# File lib/hashie/extensions/stringify_keys.rb, line 45
def stringify_keys!(hash)
  hash.extend(Hashie::Extensions::StringifyKeys) unless hash.respond_to?(:stringify_keys!)
  hash.keys.each do |k|
    stringify_keys_recursively!(hash[k])
    hash[k.to_s] = hash.delete(k)
  end
  hash
end
stringify_keys_recursively!(object) click to toggle source

Stringify all keys recursively within nested hashes and arrays. @api private

# File lib/hashie/extensions/stringify_keys.rb, line 25
def stringify_keys_recursively!(object)
  case object
  when self.class
    stringify_keys!(object)
  when ::Array
    object.each do |i|
      stringify_keys_recursively!(i)
    end
  when ::Hash
    stringify_keys!(object)
  end
end