Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - String Operations
Identify the error in this Ruby code snippet:
str = "example"
puts str[2..5, 2]
AReturns nil because of invalid range
BReturns 'am' as expected
CSyntaxError due to wrong slicing syntax
DReturns 'amp' substring
Step-by-Step Solution
Solution:
  1. Step 1: Analyze slicing syntax used

    The code uses str[2..5, 2] which mixes range and length parameters incorrectly. Ruby does not support this syntax.
  2. Step 2: Understand correct slicing methods

    Use either str[2..5] for range or str[2, 2] for start and length. Mixing both causes a syntax error.
  3. Final Answer:

    SyntaxError due to wrong slicing syntax -> Option C
  4. Quick Check:

    Invalid slice syntax = SyntaxError [OK]
Quick Trick: Use either range or start,length, not both together [OK]
Common Mistakes:
  • Mixing range and length in one slice
  • Expecting multiple parameters in range slice
  • Ignoring Ruby's slice method rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes