Iterate over Ruby Array in increments of 4 -
i have following array:
csv_array = [1,2,3,4,5,6,7,8,9,10]
i need write each item in array separate csv row, in groups of 4. if this
csv.open("content_file.csv", "wb") |csv| csv << csv_array end
i csv file of entire array laid out across 1 row.
i need csv file this:
1,2,3,4
5,6,7,8
9,10
how can write ruby script
csv << csv_array[0..3]
csv << csv_array[4..7]
and on, regardless of how many items in array? using ruby 1.9.3.
csv_array = [1,2,3,4,5,6,7,8,9,10] csv_array.each_slice(4) |chunk| p chunk end # >> [1, 2, 3, 4] # >> [5, 6, 7, 8] # >> [9, 10]
Comments
Post a Comment