0
0
Rubyprogramming~10 mins

Class.new for dynamic class creation in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class.new for dynamic class creation
Call Class.new
Create new anonymous class
Optionally add methods inside block
Assign class to variable
Create instances with .new
Use instances as normal objects
This flow shows how calling Class.new creates a new class dynamically, optionally adding methods, then assigning it to a variable to create instances.
Execution Sample
Ruby
MyClass = Class.new do
  def greet
    "Hello!"
  end
end
obj = MyClass.new
puts obj.greet
Creates a new class with a greet method, makes an instance, and prints the greeting.
Execution Table
StepActionEvaluationResult
1Call Class.new with blockCreates new anonymous classClass object assigned to MyClass
2Define method greet inside blockMethod added to new classMyClass has greet method
3Call MyClass.newCreates instance of MyClassobj is instance of MyClass
4Call obj.greetRuns greet methodReturns string "Hello!"
5puts outputPrints to consoleHello!
💡 Program ends after printing greeting
Variable Tracker
VariableStartAfter Step 1After Step 3Final
MyClassnilClass object (anonymous class)Class objectClass object
objnilnilInstance of MyClassInstance of MyClass
Key Moments - 3 Insights
Why do we assign Class.new to a variable?
Assigning the new class to a variable like MyClass lets us create instances and call methods on it later, as shown in execution_table step 1 and 3.
How does the block inside Class.new add methods?
The block runs in the context of the new class, so defining methods inside it adds them to the class, as seen in step 2.
What type is obj after MyClass.new?
obj is an instance of the dynamically created class MyClass, confirmed in step 3 and tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling obj.greet at step 4?
A"Hello!"
BAn error
Cnil
DClass object
💡 Hint
Check execution_table row 4 where obj.greet returns "Hello!"
At which step is the new class assigned to MyClass?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
See execution_table row 1 where Class.new returns the class assigned to MyClass
If we remove the block from Class.new, what happens to the greet method?
AIt is still defined
BIt is not defined
CAn error occurs immediately
DThe class cannot be created
💡 Hint
Methods are added inside the block as shown in step 2; without the block, no methods are added
Concept Snapshot
Class.new creates a new anonymous class dynamically.
You can add methods inside a block passed to Class.new.
Assign the class to a variable to use it later.
Create instances with .new on that variable.
Instances behave like normal objects of that class.
Full Transcript
This example shows how Ruby's Class.new method creates a new class dynamically. First, Class.new is called with a block that defines a greet method. This creates an anonymous class with that method. The class is assigned to the variable MyClass. Then, calling MyClass.new creates an instance obj of that class. Calling obj.greet runs the greet method and returns "Hello!". Finally, puts prints the greeting. Variables MyClass and obj change from nil to class and instance respectively. This dynamic class creation lets you build classes on the fly and use them like normal classes.