Quick Answer
Invalid string indexing or manipulation
Understanding the Issue
This occurs when trying to access string indexes that don't exist or using invalid operations like [] with regex.
The Problem
This code demonstrates the issue:
Ruby
Error
# Problem: Invalid index
"hello"[10] # nil
"hello".index("z") # nil
# Problem: Invalid [] usage
"hello"[/z/] # IndexError
The Solution
Here's the corrected code:
Ruby
Fixed
# Solution: Check bounds/patterns
str = "hello"
str[1] if str.length > 1
# Safe regex match
if match = str.match(/e/)
match[0]
end
Key Takeaways
Validate string bounds before access.