class Array

Public Instance Methods

chunks( number_of_chunks ) click to toggle source
# 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 ) click to toggle source

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) ???

# 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