module Refinements::Hashes

Refinements for Hashes.

Public Instance Methods

deep_merge(other) click to toggle source
# File lib/refinements/hashes.rb, line 34
def deep_merge other
  dup.deep_merge! other
end
deep_merge!(other) click to toggle source
# File lib/refinements/hashes.rb, line 38
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
except(*keys) click to toggle source
# File lib/refinements/hashes.rb, line 8
def except *keys
  reject { |key, _value| keys.include? key }
end
except!(*keys) click to toggle source
# File lib/refinements/hashes.rb, line 12
def except! *keys
  replace except(*keys)
end
reverse_merge(other) click to toggle source
# File lib/refinements/hashes.rb, line 52
def reverse_merge other
  other.merge self
end
reverse_merge!(other) click to toggle source
# File lib/refinements/hashes.rb, line 56
def reverse_merge! other
  merge!(other) { |_key, old_value, _new_value| old_value }
end
slice(*keys) click to toggle source
# File lib/refinements/hashes.rb, line 26
def slice *keys
  select { |key, _value| keys.include? key }
end
slice!(*keys) click to toggle source
# File lib/refinements/hashes.rb, line 30
def slice! *keys
  replace slice(*keys)
end
symbolize_keys() click to toggle source
# File lib/refinements/hashes.rb, line 16
def symbolize_keys
  dup.symbolize_keys!
end
symbolize_keys!() click to toggle source

rubocop:disable Performance/HashEachMethods

# File lib/refinements/hashes.rb, line 21
def symbolize_keys!
  keys.each { |key| self[key.to_sym] = delete key }
  self
end
use() { |values| ... } click to toggle source
# File lib/refinements/hashes.rb, line 60
def use &block
  return [] unless block_given?

  values = block.parameters.map { |(_type, key)| self[key] }
  yield values
end