Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between the Ruby range operators .. and ...?
easy
A. .. includes the end value, ... excludes the end value
B. .. excludes the end value, ... includes the end value
C. .. creates an array, ... creates a hash
D. .. is used for strings only, ... is used for numbers only

Solution

  1. Step 1: Understand the .. operator

    The .. operator creates a range that includes the last value in the sequence.
  2. Step 2: Understand the ... operator

    The ... operator creates a range that excludes the last value, stopping just before it.
  3. Final Answer:

    .. includes the end value, ... excludes the end value -> Option A
  4. Quick Check:

    Range inclusion = .., exclusion = ... [OK]
Hint: Remember: two dots include, three dots exclude end [OK]
Common Mistakes:
  • Confusing which operator includes or excludes the end
  • Thinking both operators behave the same
  • Assuming ranges create arrays automatically
  • Mixing up usage with different data types
2. Which of the following is the correct syntax to create a range from 1 to 5, excluding 5, in Ruby?
easy
A. range = 1..5
B. range = 1...5
C. range = (1..5).exclude_end?
D. range = 1-5

Solution

  1. Step 1: Identify the operator for excluding the end

    The ... operator excludes the end value, so 1...5 means 1, 2, 3, 4.
  2. Step 2: Check other options

    1..5 includes 5, (1..5).exclude_end? is a method call, not range creation, and 1-5 is subtraction.
  3. Final Answer:

    range = 1...5 -> Option B
  4. Quick Check:

    Exclude end uses three dots ... [OK]
Hint: Use three dots to exclude the last number [OK]
Common Mistakes:
  • Using two dots when exclusion is needed
  • Confusing method calls with range syntax
  • Using subtraction instead of range
  • Forgetting parentheses for method calls
3. What will be the output of the following Ruby code?
puts (1..3).to_a.inspect
puts (1...3).to_a.inspect
medium
A. [1, 2] [1, 2]
B. [1, 2] [1, 2, 3]
C. [1, 2, 3] [1, 2, 3]
D. [1, 2, 3] [1, 2]

Solution

  1. Step 1: Convert (1..3) to array

    The range 1..3 includes 3, so (1..3).to_a is [1, 2, 3].
  2. Step 2: Convert (1...3) to array

    The range 1...3 excludes 3, so (1...3).to_a is [1, 2].
  3. Final Answer:

    [1, 2, 3] [1, 2] -> Option D
  4. Quick Check:

    Two dots include end, three dots exclude end [OK]
Hint: Remember to_a shows all included values in the range [OK]
Common Mistakes:
  • Assuming both ranges include the end
  • Mixing up which operator excludes the end
  • Forgetting to convert range to array for output
  • Misreading the output format
4. The following Ruby code is intended to print numbers from 1 to 5, excluding 5. What is the error?
for i in 1..5
puts i
end
medium
A. The for loop syntax is incorrect
B. The loop variable i is not defined
C. The range should use ... to exclude 5
D. The puts statement should be outside the loop

Solution

  1. Step 1: Analyze the range used in the loop

    The range 1..5 includes 5, so the loop prints 1 through 5.
  2. Step 2: Identify how to exclude 5

    To exclude 5, use the ... operator: 1...5 which stops before 5.
  3. Final Answer:

    The range should use ... to exclude 5 -> Option C
  4. Quick Check:

    Exclude end with three dots ... [OK]
Hint: Use three dots to exclude the last number in loops [OK]
Common Mistakes:
  • Using two dots when exclusion is needed
  • Thinking loop variable needs declaration
  • Misunderstanding for loop syntax in Ruby
  • Placing puts outside the loop incorrectly
5. You want to create a Ruby method that returns all numbers from 1 up to but not including a given number n. Which range operator should you use inside the method to achieve this?
hard
A. Use 1...n to exclude n
B. Use 1...n-1 to exclude n and n-1
C. Use 1...(n+1) to exclude n
D. Use 1..n to include n

Solution

  1. Step 1: Understand the requirement

    The method should return numbers from 1 up to but not including n.
  2. Step 2: Choose the correct range operator

    Using 1...n creates a range that excludes n, exactly what is needed.
  3. Step 3: Evaluate other options

    1..n includes n, 1...(n+1) includes n, 1...n-1 excludes n-1 too, which is incorrect.
  4. Final Answer:

    Use 1...n to exclude n -> Option A
  5. Quick Check:

    Three dots exclude the end value [OK]
Hint: Use three dots to exclude the upper limit without math [OK]
Common Mistakes:
  • Using two dots and including the end value
  • Subtracting 1 manually instead of using three dots
  • Misplacing parentheses causing wrong range
  • Using three dots with arithmetic causing off-by-one
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard