Add method to Hash class to remove one hash from another : Hash Extensions « Hash « Ruby






Add method to Hash class to remove one hash from another


class Hash
  def remove_hash(other_hash)
    delete_if { |k,v| other_hash[k] == v }
  end
end

squares = { 1 => 1, 2 => 4, 3 => 9 }
doubles = { 1 => 2, 2 => 4, 3 => 6 }
squares.remove_hash(doubles)
squares                                   # => {1=>1, 3=>9}

 








Related examples in the same category

1.Add method hash class to invert a hash: turn value to key
2.Grep a hash
3.Find all