📖 Scenario: You are helping a teacher prepare a list of student roll numbers for attendance. The roll numbers go from 1 to 5. Then, you want to count backwards from 5 to 1 to check the order.
🎯 Goal: Build a Ruby program that uses upto to count from 1 to 5 and downto to count back from 5 to 1, printing each number.
📋 What You'll Learn
Create a variable start_num with the value 1
Create a variable end_num with the value 5
Use upto method on start_num to print numbers from 1 to 5
Use downto method on end_num to print numbers from 5 down to 1
💡 Why This Matters
🌍 Real World
Counting sequences like roll numbers, pages, or steps is common in many programs. Using <code>upto</code> and <code>downto</code> helps automate these counts easily.
💼 Career
Understanding these methods is useful for Ruby developers working on tasks involving loops, ranges, and iteration, common in web development and scripting.
Progress0 / 4 steps
1
Create the starting number variable
Create a variable called start_num and set it to 1.
Ruby
Hint
Use = to assign the value 1 to start_num.
2
Create the ending number variable
Create a variable called end_num and set it to 5.
Ruby
Hint
Use = to assign the value 5 to end_num.
3
Use upto to print numbers from 1 to 5
Use the upto method on start_num with end_num to print each number from 1 to 5. Use a block with variable num and print num inside the block.
Ruby
Hint
Use start_num.upto(end_num) do |num| ... end and inside the block use puts num.
4
Use downto to print numbers from 5 down to 1
Use the downto method on end_num with start_num to print each number from 5 down to 1. Use a block with variable num and print num inside the block.
Ruby
Hint
Use end_num.downto(start_num) do |num| ... end and inside the block use puts num.
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
Step 1: Understand the upto method
The upto method counts upwards from the starting number to the ending number, including both.
Step 2: Apply to the example 5.upto(8)
This means it counts 5, 6, 7, 8 in order.
Final Answer:
Counts from 5 up to 8, including both 5 and 8 -> Option C
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
Step 1: Identify correct method for counting down
To count down from 10 to 7, use downto starting at 10 and ending at 7.
Step 2: Check syntax correctness
10.downto(7) { |i| puts i } correctly counts down and prints each number.
Final Answer:
10.downto(7) { |i| puts i } -> Option A
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
Step 1: Understand the loop range
3.upto(5) iterates over 3, 4, 5.
Step 2: Calculate sum of values
sum = 0 + 3 + 4 + 5 = 12.
Final Answer:
12 -> Option A
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
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.
Step 2: Determine output
Since 5 is already greater than 3, the block does not run and no output is produced.
Final Answer:
No output because start is greater than end -> Option B
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
Step 1: Choose correct method for counting down
To count down from 10 to 2, use downto starting at 10 and ending at 2.
Step 2: Filter even numbers inside the block
Use if i.even? to add only even numbers to the array.
Final Answer:
arr = []
10.downto(2) { |i| arr << i if i.even? } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.