Bird
Raised Fist0
Rubyprogramming~20 mins

Class.new for dynamic class creation in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using Class.new?
Consider this Ruby code that creates a class dynamically and uses it:
Ruby
MyClass = Class.new do
  def greet
    "Hello from dynamic class!"
  end
end

obj = MyClass.new
puts obj.greet
AHello from dynamic class!
BNoMethodError
CSyntaxError
Dnil
Attempts:
2 left
💡 Hint
Look at how the method greet is defined inside the Class.new block.
❓ Predict Output
intermediate
2:00remaining
What does this code print when using Class.new with inheritance?
This Ruby code creates a subclass dynamically. What is printed?
Ruby
class Parent
  def message
    "Parent says hi"
  end
end

Child = Class.new(Parent) do
  def message
    super + " and Child says hello"
  end
end

puts Child.new.message
ANoMethodError
BChild says hello
CParent says hi
DParent says hi and Child says hello
Attempts:
2 left
💡 Hint
Remember that super calls the method from the parent class.
🔧 Debug
advanced
2:00remaining
Why does this dynamic class creation code raise an error?
This Ruby code tries to create a class dynamically but raises an error. Identify the cause.
Ruby
MyClass = Class.new do
  def initialize(name)
    @name = name
  end
end

obj = MyClass.new
puts obj.instance_variable_get(:@name)
AArgumentError: wrong number of arguments (given 0, expected 1)
BSyntaxError: unexpected end-of-input
CNoMethodError: undefined method 'initialize'
Dnil
Attempts:
2 left
💡 Hint
Check how many arguments are passed to new versus initialize method parameters.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a dynamic class with a class method using Class.new?
Select the Ruby code that correctly creates a class with a class method 'info' returning 'Dynamic class'.
A
MyClass = Class.new do
  def self.info
    'Dynamic class'
  end
end
B
MyClass = Class.new do
  def info
    'Dynamic class'
  end
end

MyClass.info
C
MyClass = Class.new do
  class << self
    def info
      'Dynamic class'
    end
  end
end
D
MyClass = Class.new do
  def info()
    'Dynamic class'
  end
end

MyClass.info
Attempts:
2 left
💡 Hint
Class methods can be defined inside 'class << self' block or with 'def self.method'.
🚀 Application
expert
2:00remaining
How many methods does this dynamically created class have?
Given this Ruby code, how many methods does the class MyClass respond to (excluding inherited methods)?
Ruby
MyClass = Class.new do
  def a
    1
  end

  def b
    2
  end

  def c
    3
  end
end

obj = MyClass.new
methods = obj.methods - Object.new.methods
puts methods.size
A1
B3
C6
D0
Attempts:
2 left
💡 Hint
Subtracting Object's methods from obj's methods leaves only methods defined in MyClass.

Practice

(1/5)
1. What does Class.new do in Ruby?
easy
A. Defines a method inside a class
B. Deletes an existing class
C. Creates a new instance of a class
D. Creates a new class dynamically at runtime

Solution

  1. Step 1: Understand the purpose of Class.new

    Class.new is used to create a new class dynamically while the program runs, without writing a full class definition.
  2. Step 2: Differentiate from other options

    It does not delete classes, create instances, or define methods alone. It creates a new class object.
  3. Final Answer:

    Creates a new class dynamically at runtime -> Option D
  4. Quick Check:

    Class.new = dynamic class creation [OK]
Hint: Class.new always creates a new class object [OK]
Common Mistakes:
  • Confusing class creation with instance creation
  • Thinking it deletes or modifies existing classes
  • Assuming it only defines methods
2. Which of the following is the correct syntax to create a new class with Class.new and add a method greet that returns "Hello"?
easy
A. MyClass = Class.new do def greet return "Hello" end end
B. MyClass = Class.new { greet() = "Hello" }
C. MyClass = Class.new { def greet; "Hello"; end }
D. MyClass = Class.new { def greet() puts "Hello" }

Solution

  1. Step 1: Check correct block syntax for Class.new

    The block can be given with curly braces or do-end. Inside, method definitions use def method_name; ...; end.
  2. Step 2: Validate method definition and return value

    MyClass = Class.new { def greet; "Hello"; end } correctly defines greet returning "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" } uses puts which prints but does not return the string.
  3. Final Answer:

    MyClass = Class.new { def greet; "Hello"; end } -> Option C
  4. Quick Check:

    Correct method syntax inside Class.new block = MyClass = Class.new { def greet; "Hello"; end } [OK]
Hint: Use def inside Class.new block with proper syntax [OK]
Common Mistakes:
  • Using puts instead of returning a string
  • Incorrect block syntax for Class.new
  • Invalid method definition syntax
3. What will be the output of this Ruby code?
MyClass = Class.new do
  def greet
    "Hi"
  end
end

obj = MyClass.new
puts obj.greet
medium
A. Hi
B. Hello
C. Error: undefined method greet
D. nil

Solution

  1. Step 1: Understand class creation and method definition

    The code creates a new class with a method greet returning "Hi".
  2. Step 2: Create instance and call method

    Creating an instance obj and calling obj.greet returns "Hi" which is printed.
  3. Final Answer:

    Hi -> Option A
  4. Quick Check:

    Method greet returns "Hi" [OK]
Hint: Method returns string; puts prints it [OK]
Common Mistakes:
  • Expecting 'Hello' instead of 'Hi'
  • Thinking method is undefined
  • Confusing return value with nil
4. Identify the error in this code snippet:
MyClass = Class.new {
  def greet
    "Hello"
  end
end

obj = MyClass.new
puts obj.greet
medium
A. Using curly braces instead of do-end for multi-line block
B. Missing parentheses in method definition
C. Cannot call new on Class.new result
D. Method greet should use puts instead of return

Solution

  1. Step 1: Check block syntax for Class.new

    Curly braces are for single-line blocks. Multi-line blocks should use do-end syntax.
  2. Step 2: Confirm other parts are correct

    Method definition syntax is valid, calling new is valid, and returning string is correct.
  3. Final Answer:

    Using curly braces instead of do-end for multi-line block -> Option A
  4. Quick Check:

    Multi-line block needs do-end [OK]
Hint: Use do-end for multi-line Class.new blocks [OK]
Common Mistakes:
  • Using curly braces for multi-line blocks
  • Thinking method needs parentheses
  • Confusing method return with puts
5. You want to create a dynamic class with Class.new that has an initializer accepting a name and a method greet returning "Hello, <name>". Which code correctly implements this?
hard
A. Person = Class.new { def initialize(name) @name = name end def greet "Hello, " + name end }
B. Person = Class.new do def initialize(name) @name = name end def greet "Hello, #{@name}" end end
C. Person = Class.new do def initialize(name) name = @name end def greet "Hello, " + @name end end
D. Person = Class.new do def initialize(name) @name = name end def greet puts "Hello, #{@name}" end end

Solution

  1. 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 = name in initialize. 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.
  2. 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 variable name. Person = Class.new do def initialize(name) @name = name end def greet puts "Hello, #{@name}" end end prints instead of returning.
  3. Final Answer:

    Person = Class.new do def initialize(name) @name = name end def greet "Hello, #{@name}" end end -> Option B
  4. Quick Check:

    Correct initializer and return string in greet [OK]
Hint: Assign @name in initialize; return string in greet [OK]
Common Mistakes:
  • Reversing assignment in initialize
  • Using puts instead of returning string
  • Wrong block syntax with curly braces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard