ruby - Splitting an array into x arrays -


i have array:

arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

i want split arr1 x slices, each slice full , equal possible.

arr2  = arr1.foo(3) # => [1, 2, 3, 4][5, 6, 7][8, 9, 10] 

each_slice opposite of want, separating array groups of x elements instead.

arr2 = arr1.each_slice(3) # => [1, 2, 3][4, 5, 6][7, 8, 9][10] 

if possible, want without using rails-specific methods in_groups.

class array   def in_groups(n)     len, rem = count.divmod(n)     (0...n).map { | | (i < rem) ? self[(len+1) * i, len + 1] : self[len * + rem, len] }   end end 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -