Return to Snippet

Revision: 1267
at September 27, 2006 09:29 by gdonald


Updated Code
# for creating sets of data for tables generally. it enables you to
# take an array and create sections with it.
#
# a = [1,2,3,4,5,6,7,8,9,10]
# b = array_chop(a,3)
# b.inspect
# "[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]"

def array_chop( data, num )
  res = Array.new
  buf = data.to_a.reverse
  ( data.size / num ).times do |row|
    tmp = Array.new
    num.times do |col|
      tmp << buf.pop
    end
    res << tmp
  end
  res << buf unless buf.size == 0
  res
end

Revision: 1266
at September 27, 2006 09:29 by gdonald


Initial Code
# for creating sets of data for tables generally. it enables you to
# take an array and create sections with it.
#
# a = [1,2,3,4,5,6,7,8,9,10]
# b = array_chop(a,3)
# b.inspect
# "[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]"

def array_chop( data, num )
  res = Array.new
  buf = data.to_a.reverse
  ( data.size / num ).times do |row|
    tmp = Array.new
    num.times do |col|
      tmp << buf.pop
    end
    res << tmp
  end
  res << buf unless buf.size == 0
  res
end

Initial URL


Initial Description


Initial Title
ruby array chop

Initial Tags
array

Initial Language
Ruby