class Hash
Hash
- Core Extensions
Public Instance Methods
Returns a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { person: { name: 'Rob', age: '28' } } hash.deep_stringify_keys # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
# File lib/bot_mob/core_ext/hash.rb, line 94 def deep_stringify_keys deep_transform_keys(&:to_s) end
Destructively converts all keys to strings. This includes the keys from the root hash and from all nested hashes and arrays.
# File lib/bot_mob/core_ext/hash.rb, line 101 def deep_stringify_keys! deep_transform_keys!(&:to_s) end
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' } } hash.deep_symbolize_keys # => {person: {name: "Rob", age: "28"}}
# File lib/bot_mob/core_ext/hash.rb, line 113 def deep_symbolize_keys deep_transform_keys { |key| key.to_sym rescue key } end
Destructively converts 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.
# File lib/bot_mob/core_ext/hash.rb, line 120 def deep_symbolize_keys! deep_transform_keys! { |key| key.to_sym rescue key } end
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' } } hash.deep_transform_keys{ |key| key.to_s.upcase } # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
# File lib/bot_mob/core_ext/hash.rb, line 75 def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end
Destructively converts all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.
# File lib/bot_mob/core_ext/hash.rb, line 82 def deep_transform_keys!(&block) _deep_transform_keys_in_object!(self, &block) end
Returns a new hash with all keys converted to strings.
hash = { name: 'Rob', age: '28' } hash.stringify_keys # => {"name"=>"Rob", "age"=>"28"}
# File lib/bot_mob/core_ext/hash.rb, line 38 def stringify_keys transform_keys(&:to_s) end
Destructively converts all keys to strings. Same as stringify_keys
, but modifies self
.
# File lib/bot_mob/core_ext/hash.rb, line 44 def stringify_keys! transform_keys!(&:to_s) end
Returns a new hash with all keys converted to symbols, as long as they respond to to_sym
.
hash = { 'name' => 'Rob', 'age' => '28' } hash.symbolize_keys # => {name: "Rob", age: "28"}
# File lib/bot_mob/core_ext/hash.rb, line 55 def symbolize_keys transform_keys { |key| key.to_sym rescue key } end
Destructively converts all keys to symbols, as long as they respond to to_sym
. Same as symbolize_keys
, but modifies self
.
# File lib/bot_mob/core_ext/hash.rb, line 62 def symbolize_keys! transform_keys! { |key| key.to_sym rescue key } end
Returns a new hash with all keys converted using the block
operation.
hash = { name: 'Rob', age: '28' } hash.transform_keys { |key| key.to_s.upcase } # => {"NAME"=>"Rob", "AGE"=>"28"}
If you do not provide a block
, it will return an Enumerator for chaining with other methods:
hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
# File lib/bot_mob/core_ext/hash.rb, line 13 def transform_keys return enum_for(:transform_keys) { size } unless block_given? result = self.class.new each_key do |key| result[yield(key)] = self[key] end result end
Destructively converts all keys using the block
operations. Same as transform_keys
but modifies self
.
# File lib/bot_mob/core_ext/hash.rb, line 24 def transform_keys! return enum_for(:transform_keys!) { size } unless block_given? keys.each do |key| self[yield(key)] = delete(key) end self end
Private Instance Methods
support methods for deep transforming nested hashes and arrays
# File lib/bot_mob/core_ext/hash.rb, line 127 def _deep_transform_keys_in_object(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| result[yield(key)] = _deep_transform_keys_in_object(value, &block) end else object end end
# File lib/bot_mob/core_ext/hash.rb, line 138 def _deep_transform_keys_in_object!(object, &block) case object when Hash object.keys.each do |key| value = object.delete(key) object[yield(key)] = _deep_transform_keys_in_object!(value, &block) end end object end