0
0
Rubyprogramming~5 mins

Upto and downto methods in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the upto method do in Ruby?
The upto method counts from a starting number up to an ending number, running a block of code for each number in that range.
Click to reveal answer
beginner
How does the downto method work in Ruby?
The downto method counts down from a starting number to an ending number, running a block of code for each number in that range.
Click to reveal answer
beginner
Example: What will this code print?<br>
5.upto(7) { |n| puts n }
It will print:<br>5<br>6<br>7<br>Because upto counts from 5 up to 7, printing each number.
Click to reveal answer
beginner
Example: What will this code print?<br>
3.downto(1) { |n| puts n }
It will print:<br>3<br>2<br>1<br>Because downto counts down from 3 to 1, printing each number.
Click to reveal answer
beginner
Can upto and downto be used without a block?
No, both methods require a block to tell Ruby what to do with each number in the count. Without a block, they do nothing.
Click to reveal answer
What does 2.upto(4) { |n| puts n } print?
A2 3 4
B4 3 2
C2 4
DNothing
Which method counts down from a number to a smaller number?
Aupto
Btimes
Cdownto
Deach
What happens if you call 5.upto(3) { |n| puts n }?
APrints 5, 4, 3
BPrints nothing
CPrints 3, 4, 5
DRaises an error
Which of these is true about downto?
AIt counts up
BIt can be used without a block
CIt only works with strings
DIt requires a block
What will 1.upto(1) { |n| puts n } print?
A1
BNothing
C1 2
DError
Explain how the upto method works and give a simple example.
Think about counting forward like climbing stairs.
You got /3 concepts.
    Describe the difference between upto and downto methods.
    One goes up, the other goes down.
    You got /4 concepts.