Bird
0
0

Why should pure functions avoid modifying variables outside their scope in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Functional Patterns in Ruby
Why should pure functions avoid modifying variables outside their scope in Ruby?
ATo ensure the function's output depends only on its input.
BTo make the function run faster.
CTo allow the function to print results.
DTo use global variables effectively.
Step-by-Step Solution
Solution:
  1. Step 1: Recall pure function principle

    Pure functions must not depend on or change outside variables to guarantee consistent output.
  2. Step 2: Match options to principle

    To ensure the function's output depends only on its input. matches this principle. Options B, C, and D do not relate to purity.
  3. Final Answer:

    To ensure the function's output depends only on its input. -> Option A
  4. Quick Check:

    Pure functions isolate side effects = To ensure the function's output depends only on its input. [OK]
Quick Trick: No outside changes means output depends only on inputs [OK]
Common Mistakes:
  • Confusing speed with purity
  • Allowing printing inside pure functions
  • Using global variables inside pure functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes