Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/saber/core_ext.rb
Instance Method Summary collapse
-
#deep_stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#deep_stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#deep_symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym
. -
#deep_symbolize_keys! ⇒ Object
Destructively convert all keys to symbols, as long as they respond to
to_sym
. -
#deep_transform_keys(&block) ⇒ Object
Return a new hash with all keys converted by the block operation.
-
#deep_transform_keys!(&block) ⇒ Object
Destructively convert all keys by using the block operation.
Instance Method Details
#deep_stringify_keys ⇒ Object
Return a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_stringify_keys
# => { "person" => { "name" => "Rob", "age" => "28" } }
39 40 41 |
# File 'lib/saber/core_ext.rb', line 39 def deep_stringify_keys deep_transform_keys{ |key| key.to_s } end |
#deep_stringify_keys! ⇒ Object
Destructively convert all keys to strings. This includes the keys from the root hash and from all nested hashes.
46 47 48 |
# File 'lib/saber/core_ext.rb', line 46 def deep_stringify_keys! deep_transform_keys!{ |key| key.to_s } end |
#deep_symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to to_sym
. This includes the keys from the root hash and from all nested hashes.
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
hash.deep_symbolize_keys
# => { person: { name: "Rob", age: "28" } }
58 59 60 |
# File 'lib/saber/core_ext.rb', line 58 def deep_symbolize_keys deep_transform_keys{ |key| key.to_sym rescue key } end |
#deep_symbolize_keys! ⇒ Object
Destructively convert all keys to symbols, as long as they respond to to_sym
. This includes the keys from the root hash and from all nested hashes.
65 66 67 |
# File 'lib/saber/core_ext.rb', line 65 def deep_symbolize_keys! deep_transform_keys!{ |key| key.to_sym rescue key } end |
#deep_transform_keys(&block) ⇒ Object
Return a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_transform_keys{ |key| key.to_s.upcase }
# => { "PERSON" => { "NAME" => "Rob", "AGE" => "28" } }
12 13 14 15 16 17 18 |
# File 'lib/saber/core_ext.rb', line 12 def deep_transform_keys(&block) result = {} each do |key, value| result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value end result end |
#deep_transform_keys!(&block) ⇒ Object
Destructively convert all keys by using the block operation. This includes the keys from the root hash and from all nested hashes.
23 24 25 26 27 28 29 |
# File 'lib/saber/core_ext.rb', line 23 def deep_transform_keys!(&block) keys.each do |key| value = delete(key) self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value end self end |