The range 1..3 includes 3, so (1..3).to_a is [1, 2, 3].
Step 2: Convert (1...3) to array
The range 1...3 excludes 3, so (1...3).to_a is [1, 2].
Final Answer:
[1, 2, 3]
[1, 2] -> Option D
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
Step 1: Analyze the range used in the loop
The range 1..5 includes 5, so the loop prints 1 through 5.
Step 2: Identify how to exclude 5
To exclude 5, use the ... operator: 1...5 which stops before 5.
Final Answer:
The range should use ... to exclude 5 -> Option C
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
Step 1: Understand the requirement
The method should return numbers from 1 up to but not including n.
Step 2: Choose the correct range operator
Using 1...n creates a range that excludes n, exactly what is needed.
Step 3: Evaluate other options
1..n includes n, 1...(n+1) includes n, 1...n-1 excludes n-1 too, which is incorrect.
Final Answer:
Use 1...n to exclude n -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.