Bird
Raised Fist0
Rubyprogramming~10 mins

Upto and downto methods in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Upto and downto methods
Start number
↓
Check condition: current <= end (upto) or current >= end (downto)?
|Yes↓
Execute block with current
↓
Update current: current + 1 (upto) or current - 1 (downto)
Back to condition check↓
End
↩Back to condition check
The methods start from a number and move up or down to another number, running code at each step until the end is reached.
Execution Sample
Ruby
1.upto(3) do |i|
  puts i
end

3.downto(1) do |i|
  puts i
end
Prints numbers from 1 to 3 using upto, then from 3 down to 1 using downto.
Execution Table
StepMethodCurrent ValueCondition CheckActionOutput
1upto11 <= 3 (true)Print 1, increment to 21
2upto22 <= 3 (true)Print 2, increment to 32
3upto33 <= 3 (true)Print 3, increment to 43
4upto44 <= 3 (false)Stop iteration
5downto33 >= 1 (true)Print 3, decrement to 23
6downto22 >= 1 (true)Print 2, decrement to 12
7downto11 >= 1 (true)Print 1, decrement to 01
8downto00 >= 1 (false)Stop iteration
💡 Iteration stops when current value goes beyond the end number (upto: current > end, downto: current < end).
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
current (upto)12344 (stops)
current (downto)32100 (stops)
Key Moments - 2 Insights
Why does the 'upto' method stop after printing 3 even though it increments to 4?
Because the condition check at step 4 (4 <= 3) is false, so the loop stops before printing 4, as shown in execution_table row 4.
In 'downto', why is 1 printed but 0 is not?
At step 7, current is 1 and condition 1 >= 1 is true, so it prints 1. At step 8, current is 0 and 0 >= 1 is false, so it stops without printing 0, as seen in execution_table rows 7 and 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current value during the third step of 'upto'?
A4
B2
C3
D1
💡 Hint
Check the 'Current Value' column for step 3 in the execution_table.
At which step does the 'downto' method stop iterating?
AStep 7
BStep 8
CStep 6
DStep 5
💡 Hint
Look at the 'Condition Check' and 'Action' columns for downto in the execution_table.
If we change '1.upto(3)' to '1.upto(4)', how many times will the block run?
A4 times
B3 times
C5 times
D2 times
💡 Hint
The block runs once for each number from start to end inclusive, see variable_tracker for current values.
Concept Snapshot
upto(end) { |i| ... } runs block from self up to end, increasing by 1 each time.
downto(end) { |i| ... } runs block from self down to end, decreasing by 1 each time.
Both include the end number.
Iteration stops when current passes the end.
Use for simple counting loops up or down.
Full Transcript
The 'upto' method starts from a number and counts up to another number, running code at each step. It checks if the current number is less than or equal to the end number, runs the block, then adds one. When the current number goes beyond the end, it stops. The 'downto' method works similarly but counts down from a number to a smaller number, subtracting one each time. Both methods include the end number in their iteration. For example, 1.upto(3) prints 1, 2, 3, then stops. 3.downto(1) prints 3, 2, 1, then stops. This visual trace shows each step, the current value, condition checks, actions, and outputs, helping beginners see exactly how these methods work.

Practice

(1/5)
1. What does the Ruby method 5.upto(8) do?
easy
A. Counts from 5 down to 8, excluding 8
B. Counts from 8 down to 5, including both 8 and 5
C. Counts from 5 up to 8, including both 5 and 8
D. Counts from 8 up to 5, excluding 5

Solution

  1. Step 1: Understand the upto method

    The upto method counts upwards from the starting number to the ending number, including both.
  2. Step 2: Apply to the example 5.upto(8)

    This means it counts 5, 6, 7, 8 in order.
  3. Final Answer:

    Counts from 5 up to 8, including both 5 and 8 -> Option C
  4. Quick Check:

    upto counts up = A [OK]
Hint: Remember: upto counts up, downto counts down [OK]
Common Mistakes:
  • Confusing upto with downto
  • Thinking it excludes the last number
  • Assuming it counts down instead of up
2. Which of the following is the correct syntax to count down from 10 to 7 using downto in Ruby?
easy
A. 10.downto(7) { |i| puts i }
B. 10.upto(7) { |i| puts i }
C. 7.downto(10) { |i| puts i }
D. 7.upto(10) { |i| puts i }

Solution

  1. Step 1: Identify correct method for counting down

    To count down from 10 to 7, use downto starting at 10 and ending at 7.
  2. Step 2: Check syntax correctness

    10.downto(7) { |i| puts i } correctly counts down and prints each number.
  3. Final Answer:

    10.downto(7) { |i| puts i } -> Option A
  4. Quick Check:

    Count down syntax = C [OK]
Hint: downto starts at bigger number, ends at smaller [OK]
Common Mistakes:
  • Using upto instead of downto for counting down
  • Reversing start and end numbers
  • Missing the block syntax
3. What is the output of this Ruby code?
sum = 0
3.upto(5) { |i| sum += i }
puts sum
medium
A. 12
B. 15
C. 9
D. Error

Solution

  1. Step 1: Understand the loop range

    3.upto(5) iterates over 3, 4, 5.
  2. Step 2: Calculate sum of values

    sum = 0 + 3 + 4 + 5 = 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 + 4 + 5 = 12 [OK]
Hint: Add all numbers from start to end inclusive [OK]
Common Mistakes:
  • Adding only start and end numbers
  • Confusing upto with downto
  • Forgetting to include the last number
4. Identify the error in this Ruby code:
5.upto(3) { |i| puts i }
medium
A. Syntax error due to wrong method usage
B. No output because start is greater than end
C. Infinite loop occurs
D. Outputs numbers from 5 down to 3

Solution

  1. Step 1: Analyze the upto method behavior

    upto counts up from start to end. Here start=5, end=3, which is invalid for counting up.
  2. Step 2: Determine output

    Since 5 is already greater than 3, the block does not run and no output is produced.
  3. Final Answer:

    No output because start is greater than end -> Option B
  4. Quick Check:

    upto with start > end = no output [OK]
Hint: upto does nothing if start > end, no error [OK]
Common Mistakes:
  • Expecting output when start > end
  • Thinking it counts down automatically
  • Assuming syntax error occurs
5. Using downto, how can you create an array of even numbers counting down from 10 to 2?
hard
A. arr = [] 2.upto(10) { |i| arr << i if i.even? }
B. arr = [] 2.downto(10) { |i| arr << i if i.even? }
C. arr = [] 10.upto(2) { |i| arr << i if i.even? }
D. arr = [] 10.downto(2) { |i| arr << i if i.even? }

Solution

  1. Step 1: Choose correct method for counting down

    To count down from 10 to 2, use downto starting at 10 and ending at 2.
  2. Step 2: Filter even numbers inside the block

    Use if i.even? to add only even numbers to the array.
  3. Final Answer:

    arr = [] 10.downto(2) { |i| arr << i if i.even? } -> Option D
  4. Quick Check:

    downto + even check = B [OK]
Hint: Use downto with condition inside block to filter [OK]
Common Mistakes:
  • Using upto instead of downto for counting down
  • Reversing start and end numbers
  • Not filtering even numbers correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard