Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Array.each / each_with_index / each_with_object

Array.each / each_with_index / each_with_object

These are loop methods that iterate over array elements in order. From the simple each for basic iteration, to each_with_index for index-aware loops, to each_with_object for accumulating results — choose the one that fits your use case.

Syntax

# Executes a block for each element.
arr.each { |element| process }
arr.each do |element|
  process
end

# Iterates with an index.
arr.each_with_index { |element, index| process }

# Iterates while maintaining an accumulator object.
arr.each_with_object(initial) { |element, object| process }

# Iterates in reverse order.
arr.reverse_each { |element| process }

Method List

MethodDescription
each { |e| ... }Executes a block for each element. Returns the original array.
each_with_index { |e, i| ... }Passes each element along with its index to the block.
each_with_object(obj) { |e, o| ... }Iterates while maintaining an initial object. Useful for building hashes or arrays.
reverse_each { |e| ... }Iterates over the array in reverse order.
each_slice(n) { |chunk| ... }Splits the array into groups of n elements and iterates over each group.
each_cons(n) { |chunk| ... }Iterates over each consecutive group of n elements, sliding one step at a time.

Sample Code

fruits = ['Apple', 'Orange', 'Grape', 'Peach']

# each: basic loop
fruits.each { |f| puts f }

# each_with_index: numbered list
fruits.each_with_index do |fruit, i|
  puts "#{i + 1}. #{fruit}"
end
# Outputs: 1. Apple, 2. Orange, ...

# each_with_object: building a hash
prices = [150, 80, 300, 200]
result = fruits.each_with_object({}) do |fruit, hash|
  hash[fruit] = prices[fruits.index(fruit)]
end
puts result.inspect
# {"Apple"=>150, "Orange"=>80, "Grape"=>300, "Peach"=>200}

# each_slice: group into sets of 3
(1..9).each_slice(3) { |group| puts group.inspect }
# Outputs: [1, 2, 3], [4, 5, 6], [7, 8, 9]

# each_cons: slide over consecutive groups of 3
(1..5).each_cons(3) { |group| puts group.inspect }
# Outputs: [1, 2, 3], [2, 3, 4], [3, 4, 5]

# reverse_each: iterate in reverse
[1, 2, 3].reverse_each { |n| print "#{n} " }  # 3 2 1

Notes

In Ruby, using each is the idiomatic style over for loops. With each, you can use next to skip to the next iteration, enabling clean Ruby-style code. each discards the block's return value and returns the original array. If you need the transformed results, use map instead.

each_with_index is handy for building numbered lists, but if you need to change the starting index, you can use each_with_object or the map.with_index(1) form instead.

each_with_object is useful when building a hash or array inside a loop. You can accomplish the same with inject (reduce), but when accumulating into a hash or array, each_with_object is more intuitive. Hash each follows the same method structure.

If you find any errors or copyright issues, please .