Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fleakr/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#extract!(*keys) ⇒ Object

Extract the matching keys from the source hash and return a new hash with those keys:

>> h = {:a => 'b', :c => 'd'}
=> {:a=>"b", :c=>"d"}
>> h.extract!(:a)
=> {:a=>"b"}
>> h
=> {:c=>"d"}


13
14
15
16
17
18
19
20
# File 'lib/fleakr/core_ext/hash.rb', line 13

def extract!(*keys)
  value = {}
  
  keys.each {|k| value.merge!({k => self[k]}) if self.has_key?(k) }
  keys.each {|k| delete(k) }
  
  value
end