Module: Hashie::Extensions::SymbolizeKeys::ClassMethods

Included in:
Hashie, Hashie::Extensions::SymbolizeKeys
Defined in:
lib/hashie/extensions/symbolize_keys.rb

Instance Method Summary collapse

Instance Method Details

#symbolize_keys(hash) ⇒ Object

Return a copy of hash with all keys converted to symbols.

Parameters:

  • hash (::Hash)


57
58
59
60
61
62
63
# File 'lib/hashie/extensions/symbolize_keys.rb', line 57

def symbolize_keys(hash)
  copy = hash.dup
  copy.extend(Hashie::Extensions::SymbolizeKeys) unless copy.respond_to?(:symbolize_keys!)
  copy.tap do |new_hash|
    symbolize_keys!(new_hash)
  end
end

#symbolize_keys!(hash) ⇒ Object

Convert all keys in hash to symbols.

Examples:

test = {'abc' => 'def'}
Hashie.symbolize_keys! test
test # => {:abc => 'def'}

Parameters:



45
46
47
48
49
50
51
52
# File 'lib/hashie/extensions/symbolize_keys.rb', line 45

def symbolize_keys!(hash)
  hash.extend(Hashie::Extensions::SymbolizeKeys) unless hash.respond_to?(:symbolize_keys!)
  hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods
    symbolize_keys_recursively!(hash[k])
    hash[convert_key(k)] = hash.delete(k)
  end
  hash
end

#symbolize_keys_recursively!(object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Symbolize all keys recursively within nested hashes and arrays.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hashie/extensions/symbolize_keys.rb', line 25

def symbolize_keys_recursively!(object)
  case object
  when self.class
    symbolize_keys!(object)
  when ::Array
    object.each do |i|
      symbolize_keys_recursively!(i)
    end
  when ::Hash
    symbolize_keys!(object)
  end
end