Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

addition for histogram aggregation >> :b=>2 + :c=>4

> :b=>2, :c=>4



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/modesty/core_ext/hash.rb', line 5

def +(other)
  hash = self.dup
  other.each do |k, v|
    if hash.include? k
      hash[k] += v
    else
      hash[k] = v
    end
  end
  hash
end

#map!Object



27
28
29
30
31
32
33
34
# File 'lib/modesty/core_ext/hash.rb', line 27

def map!
  self.keys.each do |k|
    v = self.delete(k)
    new_k, new_v = yield [k,v]
    self[new_k] = new_v
  end
  self
end

#map_values(&blk) ⇒ Object



23
24
25
# File 'lib/modesty/core_ext/hash.rb', line 23

def map_values(&blk)
  self.dup.map_values!(&blk)
end

#map_values!(&blk) ⇒ Object



17
18
19
20
21
# File 'lib/modesty/core_ext/hash.rb', line 17

def map_values!(&blk)
  self.each do |k,v|
    self[k] = yield(v)
  end
end

#to_hObject



36
37
38
# File 'lib/modesty/core_ext/hash.rb', line 36

def to_h
  self
end