Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_set_operators.rb

Instance Method Summary collapse

Instance Method Details

#&(hash) ⇒ Object

Returns a new hash that is a copy of the original, removing any keys that do not appear in the second hash.

{:a => 1, :b => 2} & {:b => 3}
# => {:b => 2}


40
41
42
43
44
45
# File 'lib/hash_set_operators.rb', line 40

def &(hash)
  (keys - hash.keys).each do |key|
    delete key
  end
  self
end

#-(hash) ⇒ Object

Hash difference returns a new hash that is a copy of the original removing any keys that also appear in the second hash.

{:a => 1, :b => 2} - {:b => 3}
# => {:a => 1}


27
28
29
30
31
32
# File 'lib/hash_set_operators.rb', line 27

def -(hash)
  hash.keys.each do |key|
    delete key
  end
  self
end