Recall & Review
beginner
What does
Class.new do in Ruby?<p><code>Class.new</code> creates a new class dynamically at runtime instead of defining it with the <code>class</code> keyword.</p>Click to reveal answer
beginner
How can you add methods to a class created with <code>Class.new</code>?<p>You can pass a block to <code>Class.new</code> and define methods inside that block, just like inside a normal class definition.</p>Click to reveal answer
intermediate
What is the difference between
Class.new and the usual class keyword?<p><code>Class.new</code> creates a class object dynamically and returns it, while <code>class</code> defines a class with a fixed name in the code.</p>Click to reveal answer
beginner
How do you create an instance of a class created with <code>Class.new</code>?Assign the result of Class.new to a variable, then call .new on that variable to create an instance.
Click to reveal answer
intermediate
Can <code>Class.new</code> take a superclass as an argument? If yes, how?<p>Yes. You can pass a superclass as an argument to <code>Class.new</code> like <code>Class.new(Superclass)</code> to create a subclass dynamically.</p>Click to reveal answer
What does
Class.new return in Ruby?✗ Incorrect
Class.new returns a new class object that you can assign to a variable or constant.
How do you define methods inside a class created with
Class.new?✗ Incorrect
Methods can be defined inside the block passed to Class.new, just like inside a normal class.
Which of the following is a correct way to create a subclass dynamically?
✗ Incorrect
Passing a superclass to Class.new creates a subclass dynamically.
How do you create an instance of a class created with
klass = Class.new?✗ Incorrect
Call .new on the class object stored in klass to create an instance.
What is a practical use of
Class.new?✗ Incorrect
Class.new is useful for dynamic class creation when class names or behavior depend on runtime data.
Explain how
Class.new can be used to create a class dynamically and add methods to it.Think about how you define a normal class and how that translates to using Class.new
You got /4 concepts.
Describe how inheritance works with
Class.new when creating subclasses dynamically.Consider how you normally write <code>class Subclass < Superclass</code> and how Class.new can do the same
You got /3 concepts.