Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#stringify_keysObject

Returns a new hash with all keys converted to strings.

{ name: "Tom", age: 50 }.stringify_keys #=> {"name"=>"Tom", "age"=>50}


7
8
9
# File 'lib/jun/active_support/core_ext/hash/transformation.rb', line 7

def stringify_keys
  transform_keys(&:to_s)
end

#stringify_keys!Object

Modifies the hash in place to convert all keys to strings. Same as stringify_keys but modifies self.



13
14
15
# File 'lib/jun/active_support/core_ext/hash/transformation.rb', line 13

def stringify_keys!
  transform_keys!(&:to_s)
end

#symbolize_keysObject

Returns a new hash with all keys converted to symbols.

{ "name" => "Tom", "age" => 50 }.symbolize_keys #=> {:name=>"Tom", :age=>50 }


20
21
22
# File 'lib/jun/active_support/core_ext/hash/transformation.rb', line 20

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

#symbolize_keys!Object

Modifies the hash in place to convert all keys to symbols. Same as symbolize_keys but modifies self.



26
27
28
# File 'lib/jun/active_support/core_ext/hash/transformation.rb', line 26

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