Module: Daru::IRuby::Helpers

Defined in:
lib/daru/iruby/helpers.rb

Class Method Summary collapse

Class Method Details

.nils_counted(array) ⇒ Object

It is complicated, but the only algo I could think of. It does [:a, nil, nil, :b, nil, :c] # =>

[[:a,3], nil, nil, [:b,2], nil, :c]

Needed by tuples_with_colspans/rowspans, which we need for pretty HTML



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/daru/iruby/helpers.rb', line 23

def nils_counted array
  grouped = [[array.first]]
  array[1..-1].each do |val|
    if val
      grouped << [val]
    else
      grouped.last << val
    end
  end
  grouped.flat_map { |items|
    [[items.first, items.count], *[nil] * (items.count - 1)]
  }
end

.tuples_with_colspans(index) ⇒ Object



13
14
15
16
17
# File 'lib/daru/iruby/helpers.rb', line 13

def tuples_with_colspans(index)
  index.sparse_tuples.transpose
       .map { |r| nils_counted(r) }
       .map(&:compact)
end

.tuples_with_rowspans(index) ⇒ Object



7
8
9
10
11
# File 'lib/daru/iruby/helpers.rb', line 7

def tuples_with_rowspans(index)
  index.sparse_tuples.transpose
       .map { |r| nils_counted(r) }
       .transpose.map(&:compact)
end