Class: Hash

Inherits:
Object show all
Defined in:
lib/hobo_support/hash.rb

Instance Method Summary collapse

Instance Method Details

#&(keys) ⇒ Object



46
47
48
49
50
# File 'lib/hobo_support/hash.rb', line 46

def &(keys)
  res = {}
  keys.each {|k| res[k] = self[k] if has_key?(k)}
  res
end

#-(keys) ⇒ Object



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

def -(keys)
  res = {}
  each_pair {|k, v| res[k] = v unless k.in?(keys)}
  res
end

#compactObject



58
59
60
61
62
# File 'lib/hobo_support/hash.rb', line 58

def compact
  res = {}
  each { |k, v| res[k] = v unless v.nil? }
  res
end

#compact!Object



64
65
66
# File 'lib/hobo_support/hash.rb', line 64

def compact!
  keys.each { |k| delete(k) if self[k].nil? }
end

#get(*args) ⇒ Object



54
55
56
# File 'lib/hobo_support/hash.rb', line 54

def get(*args)
  args.map {|a| self[a] }
end

#map_hash(&b) ⇒ Object



10
11
12
13
14
# File 'lib/hobo_support/hash.rb', line 10

def map_hash(&b)
  res = {}
  each {|k,v| res[k] = b.arity == 1 ? yield(v) : yield(k, v) }
  res
end

#partition_hash(keys = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hobo_support/hash.rb', line 16

def partition_hash(keys=nil)
  yes = {}
  no = {}
  each do |k,v|
    if block_given? ? yield(k,v) : keys.include?(k)
      yes[k] = v
    else
      no[k] = v
    end
  end
  [yes, no]
end

#recursive_update(hash) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/hobo_support/hash.rb', line 29

def recursive_update(hash)
  hash.each_pair do |key, value|
    current = self[key]
    if current.is_a?(Hash) and value.is_a?(Hash)
      current.recursive_update(value)
    else
      self[key] = value
    end
  end
end

#select_hash(&b) ⇒ Object



3
4
5
6
7
# File 'lib/hobo_support/hash.rb', line 3

def select_hash(&b)
  res = {}
  each {|k,v| res[k] = v if (b.arity == 1 ? yield(v) : yield(k, v)) }
  res
end