Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/neb/core_ext.rb
Instance Method Summary collapse
-
#camelize_keys(first_letter = :upper) ⇒ Object
Returns a new hash with all keys converted to UpperCamelCase strings.If th is set to
:lower
then camelize produces lowerCamelCase. -
#camelize_keys!(first_letter = :upper) ⇒ Object
Destructively converts all keys to strings.
-
#deep_camelize_keys(first_letter = :upper) ⇒ Object
Returns a new hash with all keys converted to UpperCamelCase strings.If th is set to
:lower
then camelize produces lowerCamelCase. -
#deep_camelize_keys!(first_letter = :upper) ⇒ Object
Destructively converts all keys to strings.
Instance Method Details
#camelize_keys(first_letter = :upper) ⇒ Object
Returns a new hash with all keys converted to UpperCamelCase strings.If th is set to :lower
then camelize produces lowerCamelCase.
hash = { my_name: 'Rob', my_age: '28' }
hash.camelize_keys
# => {"MyName"=>"Rob", "MyAge"=>"28"}
hash.camelize_keys(:lower)
# => {"myName"=>"Rob", "myAge"=>"28"}
15 16 17 |
# File 'lib/neb/core_ext.rb', line 15 def camelize_keys(first_letter = :upper) transform_keys { |key| key.to_s.camelize(first_letter) rescue key } end |
#camelize_keys!(first_letter = :upper) ⇒ Object
Destructively converts all keys to strings. Same as camelize_keys
, but modifies self
.
21 22 23 |
# File 'lib/neb/core_ext.rb', line 21 def camelize_keys!(first_letter = :upper) transform_keys! { |key| key.to_s.camelize(first_letter) rescue key } end |
#deep_camelize_keys(first_letter = :upper) ⇒ Object
Returns a new hash with all keys converted to UpperCamelCase strings.If th is set to :lower
then camelize produces lowerCamelCase. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { my_name: 'Rob', my_age: '28', my_friend: { his_name: "bob" } }
hash.deep_camelize_keys
# => {"MyName"=>"Rob", "MyAge"=>"28", "MyFriend"=>{"HisName"=>"bob"}}
34 35 36 |
# File 'lib/neb/core_ext.rb', line 34 def deep_camelize_keys(first_letter = :upper) deep_transform_keys { |key| key.to_s.camelize(first_letter) rescue key } end |
#deep_camelize_keys!(first_letter = :upper) ⇒ Object
Destructively converts all keys to strings. Same as camelize_keys
, but modifies self
. This includes the keys from the root hash and from all nested hashes and arrays.
42 43 44 |
# File 'lib/neb/core_ext.rb', line 42 def deep_camelize_keys!(first_letter = :upper) deep_transform_keys! { |key| key.to_s.camelize(first_letter) rescue key } end |