Class.new for dynamic class creation in Ruby - Time & Space Complexity
When we create classes dynamically using Class.new, it is important to understand how the time to create these classes grows as we create more of them.
We want to know how the time needed changes when we make many classes this way.
Analyze the time complexity of the following code snippet.
classes = []
10.times do
klass = Class.new do
def greet
"Hello"
end
end
classes << klass
end
This code creates 10 new classes dynamically, each with a simple method, and stores them in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new class with
Class.newinside a loop. - How many times: The loop runs 10 times, so the class creation happens 10 times.
Each time we add one more class creation, the total time increases by roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 class creations |
| 100 | 100 class creations |
| 1000 | 1000 class creations |
Pattern observation: The time grows steadily and directly with the number of classes created.
Time Complexity: O(n)
This means if you create twice as many classes, it will take about twice as long.
[X] Wrong: "Creating classes dynamically is instant and does not depend on how many we make."
[OK] Correct: Each class creation takes some time, so making more classes adds up and takes longer overall.
Understanding how dynamic class creation scales helps you reason about code that builds things on the fly, a useful skill in many programming tasks.
"What if we added methods inside the class dynamically for each class? How would the time complexity change?"
Practice
Class.new do in Ruby?Solution
Step 1: Understand the purpose of
Class.newClass.newis used to create a new class dynamically while the program runs, without writing a full class definition.Step 2: Differentiate from other options
It does not delete classes, create instances, or define methods alone. It creates a new class object.Final Answer:
Creates a new class dynamically at runtime -> Option DQuick Check:
Class.new = dynamic class creation [OK]
- Confusing class creation with instance creation
- Thinking it deletes or modifies existing classes
- Assuming it only defines methods
Class.new and add a method greet that returns "Hello"?Solution
Step 1: Check correct block syntax for Class.new
The block can be given with curly braces or do-end. Inside, method definitions usedef method_name; ...; end.Step 2: Validate method definition and return value
MyClass = Class.new { def greet; "Hello"; end } correctly definesgreetreturning "Hello". MyClass = Class.new do def greet return "Hello" end end is correct syntax but missing the 'end' for the block in MyClass = Class.new do def greet return "Hello" end end, so MyClass = Class.new do def greet return "Hello" end end is invalid as given. MyClass = Class.new { greet() = "Hello" } uses invalid method syntax. MyClass = Class.new { def greet() puts "Hello" } usesputswhich prints but does not return the string.Final Answer:
MyClass = Class.new { def greet; "Hello"; end } -> Option CQuick Check:
Correct method syntax inside Class.new block = MyClass = Class.new { def greet; "Hello"; end } [OK]
- Using puts instead of returning a string
- Incorrect block syntax for Class.new
- Invalid method definition syntax
MyClass = Class.new do
def greet
"Hi"
end
end
obj = MyClass.new
puts obj.greetSolution
Step 1: Understand class creation and method definition
The code creates a new class with a methodgreetreturning "Hi".Step 2: Create instance and call method
Creating an instanceobjand callingobj.greetreturns "Hi" which is printed.Final Answer:
Hi -> Option AQuick Check:
Method greet returns "Hi" [OK]
- Expecting 'Hello' instead of 'Hi'
- Thinking method is undefined
- Confusing return value with nil
MyClass = Class.new {
def greet
"Hello"
end
end
obj = MyClass.new
puts obj.greetSolution
Step 1: Check block syntax for Class.new
Curly braces are for single-line blocks. Multi-line blocks should use do-end syntax.Step 2: Confirm other parts are correct
Method definition syntax is valid, calling new is valid, and returning string is correct.Final Answer:
Using curly braces instead of do-end for multi-line block -> Option AQuick Check:
Multi-line block needs do-end [OK]
- Using curly braces for multi-line blocks
- Thinking method needs parentheses
- Confusing method return with puts
Class.new that has an initializer accepting a name and a method greet returning "Hello, <name>". Which code correctly implements this?Solution
Step 1: Check initializer assigns instance variable
Person = Class.new do def initialize(name) @name = name end def greet "Hello, #{@name}" end end correctly assigns@name = nameininitialize. Person = Class.new do def initialize(name) name = @name end def greet "Hello, " + @name end end reverses assignment incorrectly. Person = Class.new { def initialize(name) @name = name end def greet "Hello, " + name end } uses wrong block syntax. Person = Class.new do def initialize(name) @name = name end def greet puts "Hello, #{@name}" end end uses puts in greet which prints but does not return the string.Step 2: Check greet method returns correct string
Person = Class.new do def initialize(name) @name = name end def greet "Hello, #{@name}" end end returns the string with interpolation. Person = Class.new { def initialize(name) @name = name end def greet "Hello, " + name end } uses undefined variablename. Person = Class.new do def initialize(name) @name = name end def greet puts "Hello, #{@name}" end end prints instead of returning.Final Answer:
Person = Class.new do def initialize(name) @name = name end def greet "Hello, #{@name}" end end -> Option BQuick Check:
Correct initializer and return string in greet [OK]
- Reversing assignment in initialize
- Using puts instead of returning string
- Wrong block syntax with curly braces
Ready to prove it?
Pass the test and this pattern moves unknown
