Ruby Arrays

Visit each value in an array.

Ruby
scores = [82, 91, 76]
scores.each do |score|
  puts score
end
Output
82
91
76

Explanation

An array holds ordered values. each yields one value at a time to its block.

Explore the dry run
Guided walkthroughReady
1scores = [82, 91, 76]
2scores.each do |score|
3 puts score
4end

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Create an ordered array.scores = [82, 91, 76]
—
each yields one score to the block at a time.scores = [82, 91, 76]
—
Print each yielded value.scores = [82, 91, 76]
82
91
76