Module: Enumerable

Defined in:
lib/zenweb/extensions.rb

Instance Method Summary collapse

Instance Method Details

#chunkObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/zenweb/extensions.rb', line 13

def chunk
  bin, result, prev = [], [], Object.new

  each do |o|
    curr = yield o

    if prev != curr then
      bin = []
      result << [curr, bin]
      prev = curr
    end

    bin << o
  end

  result
end

#multi_group_byObject



31
32
33
34
35
36
37
38
39
# File 'lib/zenweb/extensions.rb', line 31

def multi_group_by
  r = Hash.new { |h,k| h[k] = [] }
  each do |o|
    Array(yield(o)).each do |k|
      r[k] << o
    end
  end
  r
end