0
0
Rubyprogramming~15 mins

Upto and downto methods in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using upto and downto Methods in Ruby
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use end_num.downto(start_num) do |num| ... end and inside the block use puts num.