Ruby Arrays
Visit each value in an array.
Ruby
scores = [82, 91, 76]
scores.each do |score|
puts score
endOutput
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
1
scores = [82, 91, 76]2
scores.each do |score|3
puts score4
endFollow 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
| Step / line | What happens | State after step | Output 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 |