How to Iterate Over an Array in Groups in Ruby and Rails
Ruby’s each_slice
and in_groups_of
methods are used to iterate over an array in groups of a certain size. The each_slice
method takes a single argument, which is the size of the group. The in_groups_of
method takes two arguments, the first is the size of the group and the second is the value that should be used to fill the groups that do not have the exact size.