×

ruby · level 1 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

repeats a job as often as you ask.

Imagine skipping with a rope. You do not write down every jump. You say jump 10 times. A works the same way: write the lines once, and Ruby repeats them. 3.times do means do this 3 times.

The lines between do and end are a , the bundle of code that gets repeated. Put a name between two bars, like |lap|, and it holds which go it is: 0, then 1, then 2.

To count from 1 instead, use : 1.upto(5) do |number| counts 1, 2, 3, 4 and 5. Lines after the end run once, after the loop has finished.

3.times do |lap|
  puts "Lap #{lap}"
end
puts "Finished!"

Ruby counts from 0, so 3 laps are numbered 0, 1 and 2.