Module: Fusu::Hash::Keys
- Included in:
- Fusu::Hash
- Defined in:
- lib/fusu/hash/keys.rb
Instance Method Summary collapse
-
#deep_stringify_keys(hash) ⇒ Object
Returns a new hash with all keys converted to strings.
-
#deep_stringify_keys!(hash) ⇒ Object
Destructively convert all keys to strings.
-
#deep_symbolize_keys(hash) ⇒ Object
Returns a new hash with all keys converted to symbols, as long as they respond to
to_sym
. -
#deep_symbolize_keys!(hash) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to
to_sym
. -
#deep_transform_keys(hash, &block) ⇒ Object
Returns a new hash with all keys converted by the block operation.
-
#deep_transform_keys!(hash, &block) ⇒ Object
Destructively convert all keys by using the block operation.
-
#stringify_keys(hash) ⇒ Object
Returns a new hash with all keys converted to strings.
-
#stringify_keys!(hash) ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys(hash) ⇒ Object
(also: #to_options)
Returns a new hash with all keys converted to symbols, as long as they respond to
to_sym
. -
#symbolize_keys!(hash) ⇒ Object
(also: #to_options!)
Destructively convert all keys to symbols, as long as they respond to
to_sym
. -
#transform_keys(hash) ⇒ Object
Returns a new hash with all keys converted using the block operation.
-
#transform_keys!(hash) ⇒ Object
Destructively convert all keys using the block operations.
Instance Method Details
#deep_stringify_keys(hash) ⇒ Object
91 92 93 |
# File 'lib/fusu/hash/keys.rb', line 91 def deep_stringify_keys(hash) deep_transform_keys(hash){ |key| key.to_s } end |
#deep_stringify_keys!(hash) ⇒ Object
Destructively convert all keys to strings. This includes the keys from the root hash and from all nested hashes and arrays.
98 99 100 |
# File 'lib/fusu/hash/keys.rb', line 98 def deep_stringify_keys!(hash) deep_transform_keys!(hash){ |key| key.to_s } end |
#deep_symbolize_keys(hash) ⇒ Object
Returns 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 and arrays.
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
Fusu::Hash.deep_symbolize_keys(hash)
# => {:person=>{:name=>"Rob", :age=>"28"}}
110 111 112 |
# File 'lib/fusu/hash/keys.rb', line 110 def deep_symbolize_keys(hash) deep_transform_keys(hash){ |key| key.to_sym rescue key } end |
#deep_symbolize_keys!(hash) ⇒ 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 and arrays.
117 118 119 |
# File 'lib/fusu/hash/keys.rb', line 117 def deep_symbolize_keys!(hash) deep_transform_keys!(hash){ |key| key.to_sym rescue key } end |
#deep_transform_keys(hash, &block) ⇒ Object
Returns a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { person: { name: 'Rob', age: '28' } }
Fusu::Hash.deep_transform_keys(hash){ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
72 73 74 |
# File 'lib/fusu/hash/keys.rb', line 72 def deep_transform_keys(hash, &block) _deep_transform_keys_in_object(hash, &block) end |
#deep_transform_keys!(hash, &block) ⇒ Object
Destructively convert all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.
79 80 81 |
# File 'lib/fusu/hash/keys.rb', line 79 def deep_transform_keys!(hash, &block) _deep_transform_keys_in_object!(hash, &block) end |
#stringify_keys(hash) ⇒ Object
35 36 37 |
# File 'lib/fusu/hash/keys.rb', line 35 def stringify_keys(hash) transform_keys(hash){ |key| key.to_s } end |
#stringify_keys!(hash) ⇒ Object
Destructively convert all keys to strings. Same as stringify_keys
, but modifies self
.
41 42 43 |
# File 'lib/fusu/hash/keys.rb', line 41 def stringify_keys!(hash) transform_keys!(hash){ |key| key.to_s } end |
#symbolize_keys(hash) ⇒ Object Also known as: to_options
52 53 54 |
# File 'lib/fusu/hash/keys.rb', line 52 def symbolize_keys(hash) transform_keys(hash){ |key| key.to_sym rescue key } end |
#symbolize_keys!(hash) ⇒ Object Also known as: to_options!
Destructively convert all keys to symbols, as long as they respond to to_sym
. Same as symbolize_keys
, but modifies self
.
59 60 61 |
# File 'lib/fusu/hash/keys.rb', line 59 def symbolize_keys!(hash) transform_keys!(hash){ |key| key.to_sym rescue key } end |
#transform_keys(hash) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/fusu/hash/keys.rb', line 10 def transform_keys(hash) return enum_for(:transform_keys, hash) unless block_given? result = ::Hash.new hash.each_key do |key| result[yield(key)] = hash[key] end result end |
#transform_keys!(hash) ⇒ Object
Destructively convert all keys using the block operations. Same as transform_keys but modifies self
.
21 22 23 24 25 26 27 |
# File 'lib/fusu/hash/keys.rb', line 21 def transform_keys!(hash) return enum_for(:transform_keys!, hash) unless block_given? hash.keys.each do |key| hash[yield(key)] = hash.delete(key) end hash end |