Class: Crystal::HashWithStringifyKeys

Inherits:
Hash
  • Object
show all
Defined in:
lib/crystal/hash_with_stringify_keys.rb

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ HashWithStringifyKeys

Returns a new instance of HashWithStringifyKeys.



3
4
5
6
7
8
9
10
# File 'lib/crystal/hash_with_stringify_keys.rb', line 3

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor).stringify_keys!
  else
    super(constructor)
  end
end

Instance Method Details

#stringify_keysObject

Return a new hash with all keys converted to strings.



29
30
31
# File 'lib/crystal/hash_with_stringify_keys.rb', line 29

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object

Convert all keys in the hash to strings.

Examples:

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


19
20
21
22
23
24
25
# File 'lib/crystal/hash_with_stringify_keys.rb', line 19

def stringify_keys!
  keys.each do |k|
    stringify_keys_recursively!(self[k])
    self[k.to_s] = self.delete(k)
  end
  self
end