/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# 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