Bird
0
0

Identify the error in this Ruby function intended to be pure:

medium📝 Debug Q6 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this Ruby function intended to be pure:
def increment(x)
  @count += 1
  x + 1
end
ADoes not return a value.
BModifies an instance variable, causing side effects.
CUses incorrect syntax for addition.
DFunction name is invalid.
Step-by-Step Solution
Solution:
  1. Step 1: Check for side effects

    The function changes @count, an instance variable, which is a side effect.
  2. Step 2: Confirm return value and syntax

    Function returns x + 1 correctly and syntax is valid.
  3. Final Answer:

    Modifies an instance variable, causing side effects. -> Option B
  4. Quick Check:

    Side effects break purity = Modifies an instance variable, causing side effects. [OK]
Quick Trick: Changing instance variables breaks pure function rules [OK]
Common Mistakes:
  • Thinking missing return causes error
  • Confusing syntax error with side effect
  • Believing function name matters for purity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes