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.

Example of each_slice

Here is an example of how to use the each_slice method to iterate over an array in groups of three:

1
2
3
4
5
6
7
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.each_slice(3) { |group| puts group }

#=> [1, 2, 3]
#=> [4, 5, 6]
#=> [7, 8, 9]
#=> [10]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
products = [
  { name: "Product 1", price: 10 },
  { name: "Product 2", price: 20 },
  { name: "Product 3", price: 30 },
  { name: "Product 4", price: 40 },
  { name: "Product 5", price: 50 },
]

# Iterate over the products in groups of three
products.each_slice(3) do |chunk|
  # Create a row for each chunk
  div = content_tag(:div, class: "row")

  # Iterate over the products in the chunk
  chunk.each do |product|
    # Create a column for each product
    div.concat(content_tag(:div, class: "col-xs-4", text: product[:name]))
  end

  # Render the row
  div
end

This code will render the following HTML:

1
2
3
4
5
6
7
8
9
<div class="row">
  <div class="col-xs-4">Product 1</div>
  <div class="col-xs-4">Product 2</div>
  <div class="col-xs-4">Product 3</div>
</div>
<div class="row">
  <div class="col-xs-4">Product 4</div>
  <div class="col-xs-4">Product 5</div>
</div>

As you can see, this code uses the each_slice method to iterate over the products in groups of three. For each group, the code creates a row with three columns, one for each product in the group. The code then renders the row.

These are just a few examples of how the each_slice method can be used to iterate over an array in groups of a certain size. The each_slice method can be used to perform a variety of tasks, such as splitting an array into smaller arrays or grouping elements of an array together.

Example of in_groups_of

Here is an example of how to use the in_groups_of method to iterate over an array in groups of three, filling the groups that do not have the exact size with the value nil:

1
2
3
4
5
6
7
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.in_groups_of(3, nil) { |group| puts group }

#=> [1, 2, 3]
#=> [4, 5, 6]
#=> [7, 8, 9]
#=> [10, nil, nil]

Here is another example of how to use the in_groups_of method to iterate over an array in groups of three, filling the groups that do not have the exact size with the value false:

1
2
3
4
5
6
7
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.in_groups_of(3, false) { |group| puts group }

#=> [1, 2, 3]
#=> [4, 5, 6]
#=> [7, 8, 9]
#=> [10, false, false]

How in_groups_of uses each_slice

The in_groups_of method internally uses the each_slice method. The in_groups_of method iterates over the array using the each_slice method, and for each group, it calls the block with the group as an argument. The in_groups_of method also uses the second argument to fill the groups that do not have the exact size.

The each_slice and in_groups_of methods are powerful tools that can be used to iterate over an array in groups of a certain size. These methods can be used to perform a variety of tasks, such as splitting an array into smaller arrays, or grouping elements of an array together.