Module: DeeplyEnumerable::HashExtension
Class Method Summary collapse
Instance Method Summary collapse
-
#deep_compact ⇒ Object
Recursively removes nil values, leaving every other value (including already-blank ones such as “”, [] and {}) untouched.
-
#deep_compact! ⇒ Object
In-place variant of #deep_compact, mirroring Hash#compact!.
-
#deep_compact_blank ⇒ Object
Recursively removes every blank value (nil, false, “”, “ ”, [], {}).
-
#deep_compact_blank! ⇒ Object
In-place variant of #deep_compact_blank, mirroring Hash#compact_blank!.
-
#deep_compact_blanked ⇒ Object
Recursively removes nils and collections that became blank through compaction, but keeps values that were already blank.
-
#deep_compact_blanked! ⇒ Object
In-place variant of #deep_compact_blanked.
-
#deep_compact_existing_blank ⇒ Object
Recursively removes nils and values that are already blank, but keeps collections that only become blank as a result of compaction.
-
#deep_compact_existing_blank! ⇒ Object
In-place variant of #deep_compact_existing_blank.
- #reverse_deep_merge(other_hash) ⇒ Object (also: #deep_reverse_merge)
- #reverse_deep_merge!(other_hash) ⇒ Object (also: #deep_reverse_merge!)
Class Method Details
.included(klass) ⇒ Object
5 6 7 8 9 |
# File 'lib/deeply_enumerable/hash.rb', line 5 def self.included klass klass.class_eval do include DeeplyEnumerable::Enumerable end end |
Instance Method Details
#deep_compact ⇒ Object
Recursively removes nil values, leaving every other value (including already-blank ones such as “”, [] and {}) untouched. This is the recursive counterpart of Ruby’s Hash#compact. docs.ruby-lang.org/en/master/Hash.html#method-i-compact
36 37 38 |
# File 'lib/deeply_enumerable/hash.rb', line 36 def deep_compact deep_compact_each(false, false) end |
#deep_compact! ⇒ Object
In-place variant of #deep_compact, mirroring Hash#compact!.
41 42 43 |
# File 'lib/deeply_enumerable/hash.rb', line 41 def deep_compact! replace(deep_compact) end |
#deep_compact_blank ⇒ Object
Recursively removes every blank value (nil, false, “”, “ ”, [], {}). This is the recursive counterpart of Rails’ Hash#compact_blank. github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/hash/keys.rb
70 71 72 |
# File 'lib/deeply_enumerable/hash.rb', line 70 def deep_compact_blank deep_compact_each(true, true) end |
#deep_compact_blank! ⇒ Object
In-place variant of #deep_compact_blank, mirroring Hash#compact_blank!.
75 76 77 |
# File 'lib/deeply_enumerable/hash.rb', line 75 def deep_compact_blank! replace(deep_compact_blank) end |
#deep_compact_blanked ⇒ Object
Recursively removes nils and collections that became blank through compaction, but keeps values that were already blank.
58 59 60 |
# File 'lib/deeply_enumerable/hash.rb', line 58 def deep_compact_blanked deep_compact_each(true, false) end |
#deep_compact_blanked! ⇒ Object
In-place variant of #deep_compact_blanked.
63 64 65 |
# File 'lib/deeply_enumerable/hash.rb', line 63 def deep_compact_blanked! replace(deep_compact_blanked) end |
#deep_compact_existing_blank ⇒ Object
Recursively removes nils and values that are already blank, but keeps collections that only become blank as a result of compaction.
47 48 49 |
# File 'lib/deeply_enumerable/hash.rb', line 47 def deep_compact_existing_blank deep_compact_each(false, true) end |
#deep_compact_existing_blank! ⇒ Object
In-place variant of #deep_compact_existing_blank.
52 53 54 |
# File 'lib/deeply_enumerable/hash.rb', line 52 def deep_compact_existing_blank! replace(deep_compact_existing_blank) end |
#reverse_deep_merge(other_hash) ⇒ Object Also known as: deep_reverse_merge
27 28 29 |
# File 'lib/deeply_enumerable/hash.rb', line 27 def reverse_deep_merge(other_hash) dup.reverse_deep_merge!(other_hash) end |
#reverse_deep_merge!(other_hash) ⇒ Object Also known as: deep_reverse_merge!
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/deeply_enumerable/hash.rb', line 11 def reverse_deep_merge!(other_hash) other_hash.each_pair do |current_key, other_value| this_value = self[current_key] self[current_key] = if this_value.is_a?(::Hash) && other_value.is_a?(::Hash) this_value = rebuild(this_value) unless this_value.respond_to?(:reverse_deep_merge) this_value.reverse_deep_merge(other_value) else key?(current_key) ? this_value : other_value end end self end |