Ruby Arrays
Visit each value in an array.
On this page
The concept
An array holds ordered values. each yields one value at a time to its block.
Code example
Ruby
scores = [82, 91, 76]
scores.each do |score|
puts score
endOutput
82 91 76
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
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 |
Behind the scenes
How Ruby processes this code
Ruby parses and executes the script. Blocks run when their methods invoke them.