Bird
0
0

Why does instance_eval allow access to private instance variables and methods of an object?

hard📝 Conceptual Q10 of 15
Ruby - Advanced Metaprogramming
Why does instance_eval allow access to private instance variables and methods of an object?
ABecause it changes the object's class to public temporarily.
BBecause it modifies the Ruby interpreter's security settings.
CBecause it creates a new object with public access.
DBecause it executes code in the context of the object, bypassing normal access controls.
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance_eval execution context

    instance_eval runs code as if inside the object itself, so it can see private variables and methods.
  2. Step 2: Clarify access control bypass

    This bypasses normal method visibility rules because the code runs within the object's scope.
  3. Final Answer:

    Because it executes code in the context of the object, bypassing normal access controls. -> Option D
  4. Quick Check:

    instance_eval bypasses access controls = Because it executes code in the context of the object, bypassing normal access controls. [OK]
Quick Trick: instance_eval runs code inside object, ignoring private access [OK]
Common Mistakes:
  • Thinking instance_eval changes class or interpreter settings
  • Assuming it creates new objects
  • Believing it temporarily makes private methods public

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes