0
0
Rubyprogramming~10 mins

Open classes (reopening classes) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Open classes (reopening classes)
Define class
Use class
Reopen class
Add/Change methods
Use modified class
You first define a class, then later reopen it to add or change methods, and finally use the updated class.
Execution Sample
Ruby
class Dog
  def bark
    "Woof!"
  end
end

class Dog
  def wag_tail
    "Wagging tail"
  end
end

puts Dog.new.bark
puts Dog.new.wag_tail
This code defines a Dog class, then reopens it to add a new method wag_tail, and calls both methods.
Execution Table
StepActionEvaluationResult
1Define class Dog with method barkDog class created with bark methodDog#bark returns "Woof!"
2Reopen class Dog to add wag_tail methodDog class updatedDog#wag_tail returns "Wagging tail"
3Create Dog instance and call barkDog.new.bark"Woof!"
4Create Dog instance and call wag_tailDog.new.wag_tail"Wagging tail"
5End of programAll methods accessibleProgram ends successfully
💡 Program ends after calling both methods on Dog instances
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Dog classundefinedDefined with bark methodReopened with wag_tail method addedHas bark and wag_tail methods
Key Moments - 2 Insights
Why can we define the Dog class twice without error?
In Ruby, reopening a class means the second definition adds to the existing class instead of replacing it, as shown in steps 1 and 2 of the execution_table.
Does reopening a class remove existing methods?
No, reopening a class adds or changes methods but keeps existing ones, so Dog has both bark and wag_tail after step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does Dog#bark return at step 3?
A"Woof!"
B"Wagging tail"
CError: method not found
Dnil
💡 Hint
Check the Result column at step 3 in the execution_table.
At which step is the wag_tail method added to Dog?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Action and Evaluation columns in the execution_table for when wag_tail is added.
If we removed the reopening of Dog class, what happens when calling wag_tail?
AReturns "Wagging tail"
BRaises NoMethodError
CReturns "Woof!"
DReturns nil
💡 Hint
Without reopening, wag_tail method does not exist; see variable_tracker and execution_table steps.
Concept Snapshot
Open classes in Ruby let you add or change methods anytime.
Define a class once, then reopen it later to add methods.
Existing methods stay unless explicitly changed.
Use reopened class normally with all methods available.
Full Transcript
This lesson shows how Ruby allows reopening classes to add or change methods after the initial definition. First, a Dog class is defined with a bark method. Then, the same Dog class is reopened to add a wag_tail method. Both methods can be called on Dog instances. The execution table traces each step: defining the class, reopening it, and calling methods. The variable tracker shows the Dog class gaining methods over time. Key moments clarify that reopening adds to the class without removing existing methods. The quiz tests understanding of method availability and behavior when reopening is omitted. This helps beginners see how Ruby classes are flexible and can grow over time.