lib/refinements/hashes.rb in refinements-3.2.0 vs lib/refinements/hashes.rb in refinements-4.0.0
- old
+ new
@@ -3,20 +3,16 @@
module Refinements
# Refinements for Hashes.
module Hashes
# rubocop:disable Metrics/BlockLength
refine Hash do
- # TODO: Remove when Ruby 2.4 is released.
- def compact
- puts "[DEPRECATION]: #compact is deprecated and is included, by default, in Ruby 2.4."
- dup.compact!
+ def except *keys
+ reject { |key, _value| keys.include? key }
end
- # TODO: Remove when Ruby 2.4 is released.
- def compact!
- puts "[DEPRECATION]: #compact! is deprecated and is included, by default, in Ruby 2.4."
- reject! { |_, value| value.nil? }
+ def except! *keys
+ replace except(*keys)
end
def symbolize_keys
dup.symbolize_keys!
end
@@ -25,42 +21,47 @@
keys.each { |key| self[key.to_sym] = delete(key) }
self
end
def slice *keys
- keys.each.with_object({}) do |key, sliced_hash|
- sliced_hash[key] = self[key] if key?(key)
- end
+ select { |key, _value| keys.include? key }
end
def slice! *keys
replace slice(*keys)
end
- def deep_merge other_hash
- dup.deep_merge! other_hash
+ def deep_merge other
+ dup.deep_merge! other
end
- def deep_merge! other_hash
- other_hash.each.with_object self do |(other_key, other_value), original_hash|
- current_value = original_hash[other_key]
- original_hash[other_key] = deep_merge_value current_value, other_value
+ def deep_merge! other
+ other.each do |(other_key, other_value)|
+ current_value = self[other_key]
+
+ self[other_key] = if current_value.is_a?(Hash) && other_value.is_a?(Hash)
+ current_value.deep_merge! other_value
+ else
+ other_value
+ end
end
+
+ self
end
- def reverse_merge other_hash
- other_hash.merge self
+ def reverse_merge other
+ other.merge self
end
- def reverse_merge! other_hash
- merge!(other_hash) { |_, left_value, _| left_value }
+ def reverse_merge! other
+ merge!(other) { |_key, old_value, _new_value| old_value }
end
- private
+ def use &block
+ return [] unless block_given?
- def deep_merge_value current_value, other_value
- return current_value.deep_merge(other_value) if current_value.is_a?(Hash) && other_value.is_a?(Hash)
- other_value
+ values = block.parameters.map { |(_type, key)| self[key] }
+ yield values
end
end
end
end