Module: CoreExt::Hash

Defined in:
lib/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#except(*picks) ⇒ Object



33
34
35
36
37
# File 'lib/core_ext/hash.rb', line 33

def except(*picks)
  result = self.dup
  result.except!(*picks)
  result
end

#except!(*picks) ⇒ Object



41
42
43
44
# File 'lib/core_ext/hash.rb', line 41

def except!(*picks)
  picks = picks.flatten
  keys.each {|key| self.delete(key) if picks.member?(key) }
end

#pick(*picks) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/core_ext/hash.rb', line 6

def pick(*picks)
  picks = picks.flatten

  mapped_picks = {}
  picks.each do |pick|
    if pick.is_a?(Hash)
      mapped_picks.merge!(pick)
    else
      mapped_picks[pick] = pick
    end
  end

  mapped_picks.inject({}) do |result, (key, new_key)|
    result[new_key] = self[key] if self.key?(key)
    result
  end
end

#pick!(*picks) ⇒ Object



26
27
28
29
# File 'lib/core_ext/hash.rb', line 26

def pick!(*picks)
  picks = picks.flatten
  keys.each {|key| self.delete(key) unless picks.member?(key) }
end