Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/naver/core_ext/hash/keys.rb
Instance Method Summary collapse
-
#camelize_keys ⇒ Object
Returns a new hash with all keys converted to camelcase strings.
-
#deep_camelize_keys ⇒ Object
Returns a new hash with all keys converted to camelcase strings.
-
#deep_underscore_keys ⇒ Object
Returns a new hash with all keys converted to underscore strings.
-
#transform_keys ⇒ Object
Returns a new hash with all keys converted using the
block
operation. -
#underscore_keys ⇒ Object
Returns a new hash with all keys converted to underscore strings.
Instance Method Details
#camelize_keys ⇒ Object
Returns a new hash with all keys converted to camelcase strings.
hash = { first_name: "Rob", last_name: "Bob" }
hash.camelize_keys
# => {:firstName=>"Rob", :lastName=>"Bob"}
28 29 30 |
# File 'lib/naver/core_ext/hash/keys.rb', line 28 def camelize_keys transform_keys { |key| camelize_key(key) } end |
#deep_camelize_keys ⇒ Object
Returns a new hash with all keys converted to camelcase strings. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { person: { first_name: "Rob", last_name: "Bob" } }
hash.deep_camelize_keys
# => {:person=>{:firstName=>"Rob", :lastName=>"Bob"}}
52 53 54 |
# File 'lib/naver/core_ext/hash/keys.rb', line 52 def deep_camelize_keys deep_transform_keys_in_object(self) { |key| camelize_key(key) } end |
#deep_underscore_keys ⇒ Object
Returns a new hash with all keys converted to underscore strings. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { person: { firstName: "Rob", lastName: "Bob" } }
hash.deep_underscore_keys
# => {:person=>{:first_name=>"Rob", :last_name=>"Bob"}}
40 41 42 |
# File 'lib/naver/core_ext/hash/keys.rb', line 40 def deep_underscore_keys deep_transform_keys_in_object(self) { |key| underscore_key(key) } end |
#transform_keys ⇒ Object
Returns a new hash with all keys converted using the block
operation.
3 4 5 6 7 8 9 10 |
# File 'lib/naver/core_ext/hash/keys.rb', line 3 def transform_keys return enum_for(:transform_keys) { size } unless block_given? result = {} each_key do |key| result[yield(key)] = self[key] end result end |
#underscore_keys ⇒ Object
Returns a new hash with all keys converted to underscore strings.
hash = { firstName: "Rob", lastName: "Bob" }
hash.underscore_keys
# => {:first_name=>"Rob", :last_name=>"Bob"}
18 19 20 |
# File 'lib/naver/core_ext/hash/keys.rb', line 18 def underscore_keys transform_keys { |key| underscore_key(key) } end |