Challenge - 5 Problems
Ruby Range Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
The .. operator creates a range that includes the last number.
✗ Incorrect
The .. operator creates an inclusive range, so 5 is included in the array.
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
The ... operator excludes the last number from the range.
✗ Incorrect
The ... operator creates an exclusive range, so 5 is not included in the array.
🧠 Conceptual
advanced2:00remaining
Difference between .. and ... in Ruby ranges
Which statement correctly describes the difference between the .. and ... range operators in Ruby?
Attempts:
2 left
💡 Hint
Think about whether the last number is included or not.
✗ Incorrect
The .. operator includes the end value in the range, while the ... operator excludes it.
❓ Predict Output
advanced2:00remaining
Output of range with characters using ... operator
What is the output of this Ruby code?
range = ('a'...'d').to_a
puts range.inspectRuby
range = ('a'...'d').to_a puts range.inspect
Attempts:
2 left
💡 Hint
The ... operator excludes the end character.
✗ Incorrect
The ... operator excludes 'd', so the array contains 'a', 'b', and 'c'.
❓ Predict Output
expert2: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
Attempts:
2 left
💡 Hint
Ranges with floats do not support to_a method.
✗ Incorrect
Ruby Range with floats does not support to_a because it cannot iterate over floats by default.