Class: Array

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

Overview

todo - move to ext/array.rb or similar

Instance Method Summary collapse

Instance Method Details

#chunks(number_of_chunks) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pluto.rb', line 111

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


107
108
109
# File 'lib/pluto.rb', line 107

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