0
0
Rubyprogramming~10 mins

Class_eval and instance_eval in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class_eval and instance_eval
Start
Call class_eval on Class
Execute block in Class context
Define or modify class methods/variables
Call instance_eval on Object
Execute block in Object's singleton class context
Define or modify singleton methods/variables
End
class_eval runs code inside a class context to add or change instance methods or variables. instance_eval runs code inside a single object's context to add or change its singleton methods or variables.
Execution Sample
Ruby
class Dog
end

Dog.class_eval do
  def bark
    "Woof!"
  end
end

fido = Dog.new
fido.instance_eval do
  def speak
    bark + "!!!"
  end
end

puts fido.speak
This code adds a bark method to Dog class using class_eval, then adds a speak method only to fido instance using instance_eval.
Execution Table
StepActionContextEffectOutput
1Define empty Dog classClass DogDog class created
2Dog.class_eval block startsDog class contextDefines method bark
3Define method barkDog class contextDog instances can bark
4Create fido = Dog.newObject fidoNew Dog instance fido
5fido.instance_eval block startsfido singleton contextDefines method speak only for fido
6Define method speakfido singleton contextfido can speak, other Dogs cannot
7Call fido.speakfido singleton contextCalls bark + '!!!'Woof!!!!
8EndProgram endsOutput printedWoof!!!!
💡 Program ends after printing fido.speak output
Variable Tracker
VariableStartAfter class_evalAfter instance_evalFinal
DogClass definedMethod bark addedNo changeClass with bark method
fidoUndefinedUndefinedMethod speak added to fido onlyDog instance with speak and bark methods
Key Moments - 2 Insights
Why does fido have speak method but other Dog instances do not?
Because speak is defined inside fido.instance_eval block, it adds speak only to fido's singleton class, not to Dog class itself (see execution_table step 5 and 6).
What does class_eval do differently than instance_eval here?
class_eval runs code inside the Dog class context, adding methods to all Dog instances (step 2 and 3), while instance_eval runs code inside a single object's context, adding methods only to that object (step 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what method is added to Dog class?
Abark
Bspeak
Cspeak and bark
DNo method added
💡 Hint
Check the 'Effect' column at step 3 in execution_table
At which step does fido gain the speak method?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look at the 'Action' and 'Effect' columns around steps 5 and 6
If we create another Dog instance named rover after instance_eval, does rover respond to speak?
AYes, because speak is added to Dog class
BNo, because speak is added only to fido's singleton class
CYes, because instance_eval affects all instances
DNo, because speak is a private method
💡 Hint
Refer to variable_tracker row for fido and Dog, and execution_table steps 5-6
Concept Snapshot
class_eval runs code inside a class to add or change methods for all instances.
instance_eval runs code inside a single object to add or change methods only for that object.
Use class_eval to add methods to a class.
Use instance_eval to add methods to one object.
Methods added by instance_eval are not shared by other instances.
Full Transcript
This visual execution shows how Ruby's class_eval and instance_eval work. First, an empty Dog class is defined. Using class_eval, a bark method is added to Dog class, so all Dog instances can bark. Then, a Dog instance named fido is created. Using instance_eval on fido, a speak method is added only to fido's singleton class, so only fido can speak. When fido.speak is called, it uses bark plus exclamation marks. Other Dog instances do not have speak method. The execution table traces each step, showing context and effects. Variable tracker shows Dog class gains bark method after class_eval, and fido gains speak method after instance_eval. Key moments clarify why speak is only for fido and difference between class_eval and instance_eval. The quiz tests understanding of when methods are added and to which objects. The snapshot summarizes usage: class_eval for class-wide changes, instance_eval for single object changes.