Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/textutils/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#chunks(number_of_chunks) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/textutils/core_ext/array.rb', line 19

def chunks( number_of_chunks )
  ## NB: use chunks - columns might be in use by ActiveRecord! 
  ###
  # e.g.
  #  [1,2,3,4,5,6,7,8,9,10].columns(3)
  #   becomes:
  #  [[1,4,7,10],
  #   [2,5,8],
  #   [3,6,9]]

  ## check/todo: make a copy of the array first??
  #  for now reference to original items get added to columns
  chunks = (1..number_of_chunks).collect { [] }
  each_with_index do |item,index|
    chunks[ index % number_of_chunks ] << item
  end
  chunks
end

#in_columns(cols) ⇒ Object

todo: check if there’s already a builtin method for this

note:
 in rails ary.in_groups(3)  results in
        top-to-bottom, left-to-right.
and not left-to-right first and than top-to-bottom.

rename to in_groups_vertical(3) ???


15
16
17
# File 'lib/textutils/core_ext/array.rb', line 15

def in_columns( cols )  # alias for convenience for chunks - needed? why? why not?
  chunks( cols )
end