Complete the code to define a method that returns a string representation of the object.
def to_s return [1] end
puts or print instead of returning a string.The to_s method should return a string. Using "Hello" returns the string correctly.
Complete the code to return the string representation of the instance variable @name.
def to_s @name[1] end
.to_i or other conversions that do not return strings.The to_s method should return a string, so we convert @name to string using .to_s.
Fix the error in the to_s method to correctly return a string representation.
def to_s "Name: " + [1] end
@name without conversion causes error if it's not a string.To concatenate strings, @name must be converted to string using .to_s.
Fill both blanks to create a to_s method that returns a formatted string with name and age.
def to_s "Name: [1], Age: [2]" end
Using string interpolation with #@name and #@age inserts the instance variables into the string.
Fill all three blanks to create a to_s method that returns a detailed string with name, age, and city.
def to_s "Name: [1], Age: [2], City: [3]" end
Use string interpolation for all instance variables inside the string to return a detailed description.