Class: Hash

Inherits:
Object show all
Defined in:
lib/fir/patches/native_patch.rb,
lib/fir/patches/native_patch.rb

Instance Method Summary collapse

Instance Method Details

#deep_symbolize_keysObject

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"}}


180
181
182
# File 'lib/fir/patches/native_patch.rb', line 180

def deep_symbolize_keys
  deep_transform_keys { |key| key.to_sym rescue key }
end

#deep_transform_keys(&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' } }

hash.deep_transform_keys{ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}


168
169
170
# File 'lib/fir/patches/native_patch.rb', line 168

def deep_transform_keys(&block)
  _deep_transform_keys_in_object(self, &block)
end

#symbolize_keysObject

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"}


156
157
158
# File 'lib/fir/patches/native_patch.rb', line 156

def symbolize_keys
  transform_keys { |key| key.to_sym rescue key }
end

#transform_keysObject

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"}


140
141
142
143
144
145
146
147
# File 'lib/fir/patches/native_patch.rb', line 140

def transform_keys
  return enum_for(:transform_keys) unless block_given?
  result = self.class.new
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end