Bird
0
0

Why does the Ruby range (1...1) produce an empty array when converted with to_a?

hard📝 Conceptual Q10 of 15
Ruby - Operators and Expressions
Why does the Ruby range (1...1) produce an empty array when converted with to_a?
ABecause triple dot includes the end making it empty
BBecause the start and end are the same and triple dot excludes the end
CBecause ranges cannot have equal start and end
DBecause <code>to_a</code> does not work on ranges with equal bounds
Step-by-Step Solution
Solution:
  1. Step 1: Understand the triple dot range (1...1)

    The triple dot excludes the end value, so it includes numbers from 1 up to but not including 1.
  2. Step 2: Result of such a range

    Since start and end are the same and end is excluded, no numbers are included, resulting in an empty array.
  3. Final Answer:

    Because the start and end are the same and triple dot excludes the end -> Option B
  4. Quick Check:

    Triple dot excludes end, empty if start=end [OK]
Quick Trick: Triple dot excludes end, empty if start equals end [OK]
Common Mistakes:
  • Thinking ranges cannot have equal start and end
  • Assuming triple dot includes the end
  • Believing to_a fails on such ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes