Complete the code to create a new class dynamically using Class.new.
MyClass = [1] do def greet 'Hello!' end end
Use Class.new to create a new class dynamically in Ruby.
Complete the code to create an instance of the dynamically created class.
obj = MyClass.[1]Use .new to create an instance of a class in Ruby.
Fix the error in the code to define a method inside the dynamically created class.
MyClass = Class.new do def [1] 'Hi there' end end
Method names in Ruby should be lowercase with underscores, so say_hello is correct.
Fill both blanks to create a dynamic class with an initialize method that sets a name attribute.
Person = Class.new do def [1](name) @[2] = name end end
The constructor method in Ruby is initialize, and instance variables start with @ followed by the attribute name like @name.
Fill all three blanks to create a dynamic class with an attribute reader and a method that returns a greeting with the name.
User = Class.new do attr_reader :[1] def initialize([2]) @[3] = [2] end def greet "Hello, #{@[1]!" end end
Use name consistently: attr_reader :name, initialize(name), @name = name. The greet method uses @name.