Class: Array

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

Instance Method Summary collapse

Instance Method Details

#to_freq_hashObject



2
3
4
5
6
7
8
# File 'lib/array_helper.rb', line 2

def to_freq_hash
  freq = {}
  each do |e|
    freq[e] = freq[e].to_i + 1
  end
  freq
end

#to_grouped_hashObject



23
24
25
26
27
28
29
30
31
# File 'lib/array_helper.rb', line 23

def to_grouped_hash
  groups = {}
  each do |e|
    key, value  = yield(e)
    groups[key] ||= []
    groups[key] << value
  end
  groups
end

#to_summarized_hash(operation_symbol = :+) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/array_helper.rb', line 10

def to_summarized_hash(operation_symbol = :+)
  sums = {}
  each do |e|
    key, value = yield(e)
    if sums[key]
      sums[key] = sums[key].send(operation_symbol, value)
    else
      sums[key] = value
    end
  end
  sums
end