Module: Util
- Defined in:
- lib/util/array.rb,
lib/util/hash_recursive.rb
Class Method Summary collapse
- .clone_recursive(hash) ⇒ Object
- .each_recursive(hash, &block) ⇒ Object
- .first_not_nil(array, default = nil) ⇒ Object
- .merge_recursive!(*hashes, overwrite: true) ⇒ Object
Class Method Details
.clone_recursive(hash) ⇒ Object
22 23 24 25 26 |
# File 'lib/util/hash_recursive.rb', line 22 def self.clone_recursive(hash) hash.transform_values do |value| value.is_a?(Hash) ? clone_recursive(value) : value end end |
.each_recursive(hash, &block) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/util/hash_recursive.rb', line 14 def self.each_recursive(hash, &block) hash.each do |key, value| next block.call(hash, key, value) unless value.is_a?(Hash) block.call(hash, key, each_recursive(value, &block)) end end |
.first_not_nil(array, default = nil) ⇒ Object
4 5 6 7 |
# File 'lib/util/array.rb', line 4 def self.first_not_nil(array, default = nil) index = indexes.find { |i| !array[i].nil? } index.nil? ? default : array[index] end |
.merge_recursive!(*hashes, overwrite: true) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/util/hash_recursive.rb', line 4 def self.merge_recursive!(*hashes, overwrite: true) hashes[0].merge!(*hashes[1..]) do |_key, old, new| if old.is_a?(Hash) && new.is_a?(Hash) merge_recursive!(old, new) else overwrite ? new : old end end end |