0
0
Rubyprogramming~20 mins

Range operators (.. and ...) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Range Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inclusive range with .. operator
What is the output of this Ruby code?
range = (1..5).to_a
puts range.inspect
Ruby
range = (1..5).to_a
puts range.inspect
A[1, 2, 3, 4, 5, 6]
B[1, 2, 3, 4]
C[1, 2, 3, 4, 5]
DSyntaxError
Attempts:
2 left
💡 Hint
The .. operator creates a range that includes the last number.
Predict Output
intermediate
2:00remaining
Output of exclusive range with ... operator
What is the output of this Ruby code?
range = (1...5).to_a
puts range.inspect
Ruby
range = (1...5).to_a
puts range.inspect
ARuntimeError
B[1, 2, 3, 4, 5]
C[1, 2, 3]
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint
The ... operator excludes the last number from the range.
🧠 Conceptual
advanced
2:00remaining
Difference between .. and ... in Ruby ranges
Which statement correctly describes the difference between the .. and ... range operators in Ruby?
AThe .. operator creates an exclusive range excluding the end value; the ... operator creates an inclusive range including the end value.
BThe .. operator creates an inclusive range including the end value; the ... operator creates an exclusive range excluding the end value.
CBoth .. and ... create inclusive ranges including the end value.
DBoth .. and ... create exclusive ranges excluding the end value.
Attempts:
2 left
💡 Hint
Think about whether the last number is included or not.
Predict Output
advanced
2:00remaining
Output of range with characters using ... operator
What is the output of this Ruby code?
range = ('a'...'d').to_a
puts range.inspect
Ruby
range = ('a'...'d').to_a
puts range.inspect
A["a", "b", "c"]
B["a", "b"]
C["a", "b", "c", "d"]
DTypeError
Attempts:
2 left
💡 Hint
The ... operator excludes the end character.
Predict Output
expert
2:00remaining
Number of elements in a range with floating point numbers
What happens when you try to convert this range to an array?
range = (1.0..5.0).to_a
puts range.inspect
Ruby
range = (1.0..5.0).to_a
puts range.inspect
ANoMethodError: undefined method `to_a' for 1.0..5.0:Range
B[1.0, 2.0, 3.0, 4.0, 5.0]
C[1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, ...]
DTypeError: can't iterate over Float range
Attempts:
2 left
💡 Hint
Ranges with floats do not support to_a method.