0
0
Rubyprogramming~5 mins

Range operators (.. and ...) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the range operator .. do in Ruby?
The .. operator creates a range that includes both the start and end values. It's called an inclusive range.
Click to reveal answer
beginner
What is the difference between .. and ... range operators in Ruby?
The .. operator includes the end value in the range (inclusive), while ... excludes the end value (exclusive).
Click to reveal answer
beginner
How would you create a range from 1 to 5 excluding 5 in Ruby?
Use the exclusive range operator: 1...5. This includes 1, 2, 3, 4 but not 5.
Click to reveal answer
beginner
What will (1..3).to_a return in Ruby?
It returns the array [1, 2, 3] because the range is inclusive of the end value 3.
Click to reveal answer
beginner
What will (1...3).to_a return in Ruby?
It returns the array [1, 2] because the range excludes the end value 3.
Click to reveal answer
Which range operator includes the end value in Ruby?
A..
B...
C-
D=>
What does (5...10).to_a return?
A[5, 6, 7, 8, 9, 10]
B[6, 7, 8, 9]
C[6, 7, 8, 9, 10]
D[5, 6, 7, 8, 9]
How do you create a range that includes numbers 1 to 3 in Ruby?
A1..3
B1...3
C1-3
D1=>3
Which of these is an exclusive range in Ruby?
A1..5
B1...5
C1-5
D1..6
What will (10..10).to_a return?
A[]
B[9, 10]
C[10]
D[10, 11]
Explain the difference between the two Ruby range operators .. and ....
Think about whether the last number is part of the range or not.
You got /3 concepts.
    How would you use Ruby ranges to create a list of numbers from 1 to 5, excluding 5?
    Use the operator that excludes the end value.
    You got /3 concepts.