What if you could count up or down with just one simple command instead of writing every number yourself?
Why Upto and downto methods in Ruby? - Purpose & Use Cases
Imagine you want to count numbers from 1 to 10 or from 10 down to 1 by writing each number manually in your code.
Writing each number by hand is slow, boring, and easy to make mistakes. If you want to change the range, you must rewrite many lines, which wastes time and causes errors.
The upto and downto methods let you count up or down easily with just one line. They save time and avoid mistakes by automating the counting process.
puts 1 puts 2 puts 3 puts 4 puts 5
1.upto(5) { |n| puts n }
With upto and downto, you can quickly repeat actions over a range of numbers without writing repetitive code.
For example, printing page numbers in a book from 1 to 100 or counting down seconds in a timer becomes simple and clean.
Manually writing repeated numbers is slow and error-prone.
upto and downto automate counting up or down.
They make your code shorter, clearer, and easier to change.