Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/core_ext/hash/hash.rb

Instance Method Summary collapse

Instance Method Details

#symbolize_keysObject

hash = { ‘name’ => ‘Rob’, ‘age’ => ‘28’ } hash.symbolize_keys

> { name: “Rob”, age: “28” }



27
28
29
# File 'lib/active_support/core_ext/hash/hash.rb', line 27

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

#symbolize_keys!Object

File activesupport/lib/active_support/core_ext/hash/keys.rb, line 135



32
33
34
# File 'lib/active_support/core_ext/hash/hash.rb', line 32

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

#transform_keysObject

File activesupport/lib/active_support/core_ext/hash/keys.rb

hash = { name: ‘Rob’, age: ‘28’ } hash.transform_keys{ |key| key.to_s.upcase }

> { “NAME” => “Rob”, “AGE” => “28” }



8
9
10
11
12
13
14
# File 'lib/active_support/core_ext/hash/hash.rb', line 8

def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

#transform_keys!(&block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/active_support/core_ext/hash/hash.rb', line 16

def transform_keys!(&block)
  keys.each do |key|
    value = delete(key)
    self[yield(key)] = value.is_a?(Hash) ? value.transform_keys!(&block) : value
  end
  self
end