Class: Array

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

Overview

todo - move to ext/array.rb or similar

Instance Method Summary collapse

Instance Method Details

#chunks(number_of_chunks) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pluto/models.rb', line 129

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


125
126
127
# File 'lib/pluto/models.rb', line 125

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