Complete the code to define a constructor method named initialize.
class Person def [1](name) @name = name end end
The constructor method in Ruby is always named initialize. It is called when a new object is created.
Complete the code to create a new Person object with the name 'Alice'.
person = Person.[1]('Alice')
To create a new object in Ruby, you call the class method new, which then calls initialize internally.
Fix the error in the initialize method to correctly assign the age parameter to an instance variable.
class Person def initialize(age) [1] = age end end
Instance variables in Ruby start with @. To store the age in the object, use @age.
Fill both blanks to define an initialize method that sets @name and @age from parameters.
class Person def initialize([1], [2]) @name = name @age = age end end
The initialize method parameters should match the instance variables you want to set. Here, name and age are used.
Fill all three blanks to create a Person class with initialize method and a method to display the name.
class Person def [1](name) @name = name end def [2] puts "Name: [3]" end end
The constructor method is initialize. The method to show the name can be called display_name. To print the instance variable, use @name.