Class: Hash
- Defined in:
- lib/ebooks_renamer/core_ext/hash/keys.rb,
lib/ebooks_renamer/core_ext/object/blank.rb
Instance Method Summary collapse
-
#reverse_merge(other_hash) ⇒ Object
Merges the caller into
other_hash. -
#reverse_merge!(other_hash) ⇒ Object
Destructive
reverse_merge. -
#symbolize_keys ⇒ Object
hash = { ‘name’ => ‘Rob’, ‘age’ => ‘28’ } hash.symbolize_keys => { name: “Rob”, age: “28” }.
-
#symbolize_keys! ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb, line 135.
-
#transform_keys ⇒ Object
File activesupport/lib/active_support/core_ext/hash/keys.rb.
- #transform_keys!(&block) ⇒ Object
Instance Method Details
#reverse_merge(other_hash) ⇒ Object
Merges the caller into other_hash. For example,
= .reverse_merge(size: 25, velocity: 10)
is equivalent to
= { size: 25, velocity: 10 }.merge()
This is particularly useful for initializing an options hash with default values.
46 47 48 |
# File 'lib/ebooks_renamer/core_ext/hash/keys.rb', line 46 def reverse_merge(other_hash) other_hash.merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
Destructive reverse_merge.
51 52 53 54 |
# File 'lib/ebooks_renamer/core_ext/hash/keys.rb', line 51 def reverse_merge!(other_hash) # right wins if there is no left merge!(other_hash) { |key, left, right| left } end |
#symbolize_keys ⇒ Object
hash = { ‘name’ => ‘Rob’, ‘age’ => ‘28’ } hash.symbolize_keys
> { name: “Rob”, age: “28” }
27 28 29 |
# File 'lib/ebooks_renamer/core_ext/hash/keys.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/ebooks_renamer/core_ext/hash/keys.rb', line 32 def symbolize_keys! transform_keys! { |key| key.to_sym rescue key } end |
#transform_keys ⇒ Object
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/ebooks_renamer/core_ext/hash/keys.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/ebooks_renamer/core_ext/hash/keys.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 |