Advanced
Ruby
Q82 / 100
What is the difference between "Object#instance_eval" and "Object#class_eval" (also "module_eval")?
Correct! Well done.
Incorrect.
The correct answer is B) instance_eval evaluates a block with self set to the receiver (useful for singleton methods or accessing privates), while class_eval evaluates a block in the context of a class/module, allowing instance method definitions
B
Correct Answer
instance_eval evaluates a block with self set to the receiver (useful for singleton methods or accessing privates), while class_eval evaluates a block in the context of a class/module, allowing instance method definitions
Explanation
instance_eval changes self to the receiver for the duration of the block, often used for DSLs or accessing privates; class_eval changes self to a class/module, allowing def to define instance methods on it dynamically.
Progress
82/100