Class: Hash
- Defined in:
- lib/extensions/deep_find.rb,
sig/lib/extensions/deep_find.rbs
Overview
:nodoc:
Instance Method Summary collapse
-
#collect_values(key) ⇒ Array
Recursively collects values for a key from nested hashes.
-
#deep_find(key, uniq: true) ⇒ Object
Hash#deep_find-> value.
Instance Method Details
#collect_values(key) ⇒ Array
Recursively collects values for a key from nested hashes.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/extensions/deep_find.rb', line 26 def collect_values(key) result = [self[key]] each_value do |value| values = value.is_a?(Array) ? value : [value] values.each do |v| result << v.deep_find(key) if v.is_a?(Hash) end end result end |
#deep_find(key, uniq: true) ⇒ Object
TODO:
change uniq true to uniq false
Hash#deep_find -> value
This method is an extension for Hash core class to search for a value of a key in N-nested hash. It provides search for multiple values if key appears more than once. For e.g.:
If values are identical, they will be returned in a single copy. You can disable this
feature with special param uniq, which is true by default. For e.g.:
9 10 11 12 13 14 15 16 17 |
# File 'lib/extensions/deep_find.rb', line 9 def deep_find(key, uniq: true) result = collect_values(key) result.compact! result.delete_if { |i| i.is_a?(Array) && i.empty? } result.uniq! if uniq return nil if result.empty? result.size == 1 ? result.first : result end |