0
0
Rubyprogramming~20 mins

To_s method for string representation in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby To_s 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 to_s?

Consider the following Ruby code:

class Item
  def initialize(name, price)
    @name = name
    @price = price
  end

  def to_s
    "Item: #{@name}, Price: $#{@price}"  
  end
end

item = Item.new("Book", 15)
puts item.to_s

What will be printed?

Ruby
class Item
  def initialize(name, price)
    @name = name
    @price = price
  end

  def to_s
    "Item: #{@name}, Price: $#{@price}"  
  end
end

item = Item.new("Book", 15)
puts item.to_s
AItem: Book, Price: $15
B#<Item:0x00007fffe30b8c20>
CBook 15
DItem Book 15
Attempts:
2 left
💡 Hint

Remember, to_s returns a string representation of the object.

Predict Output
intermediate
2:00remaining
What does this to_s method output for a custom class?

Look at this Ruby code:

class Person
  def initialize(name, age)
    @name = name
    @age = age
  end

  def to_s
    "#{@name} is #{@age} years old"
  end
end

p = Person.new("Alice", 30)
puts p

What will be printed?

Ruby
class Person
  def initialize(name, age)
    @name = name
    @age = age
  end

  def to_s
    "#{@name} is #{@age} years old"
  end
end

p = Person.new("Alice", 30)
puts p
APerson: Alice, 30
B#<Person:0x00007fffe30b8c20>
CAlice is 30 years old
DAlice 30
Attempts:
2 left
💡 Hint

When you puts an object, Ruby calls to_s automatically.

🔧 Debug
advanced
2:00remaining
Why does this to_s method cause an error?

Examine this Ruby code:

class Product
  def initialize(name, price)
    @name = name
    @price = price
  end

  def to_s
    "Product: #{@name}, Price: $#{price}"
  end
end

p = Product.new("Pen", 2)
puts p.to_s

What error will this code raise?

Ruby
class Product
  def initialize(name, price)
    @name = name
    @price = price
  end

  def to_s
    "Product: #{@name}, Price: $#{price}"
  end
end

p = Product.new("Pen", 2)
puts p.to_s
ANameError: undefined local variable or method `price' for #<Product:0x00007fffe30b8c20>
BSyntaxError: unexpected end-of-input
CTypeError: no implicit conversion of Integer into String
DNoMethodError: undefined method `to_s' for Product
Attempts:
2 left
💡 Hint

Check variable names inside string interpolation.

🧠 Conceptual
advanced
1:30remaining
What is the purpose of overriding to_s in Ruby classes?

Why do Ruby programmers override the to_s method in their classes?

ATo change the class name of the object
BTo provide a meaningful string representation of the object for printing or logging
CTo prevent the object from being converted to a string
DTo automatically save the object to a file
Attempts:
2 left
💡 Hint

Think about what puts object shows by default.

Predict Output
expert
2:30remaining
What is the output of this Ruby code with nested to_s calls?

Analyze this Ruby code:

class Point
  def initialize(x, y)
    @x = x
    @y = y
  end

  def to_s
    "(#{@x}, #{@y})"
  end
end

class Line
  def initialize(start_point, end_point)
    @start_point = start_point
    @end_point = end_point
  end

  def to_s
    "Line from #{@start_point.to_s} to #{@end_point.to_s}"
  end
end

p1 = Point.new(1, 2)
p2 = Point.new(3, 4)
line = Line.new(p1, p2)
puts line.to_s

What will be printed?

Ruby
class Point
  def initialize(x, y)
    @x = x
    @y = y
  end

  def to_s
    "(#{@x}, #{@y})"
  end
end

class Line
  def initialize(start_point, end_point)
    @start_point = start_point
    @end_point = end_point
  end

  def to_s
    "Line from #{@start_point.to_s} to #{@end_point.to_s}"
  end
end

p1 = Point.new(1, 2)
p2 = Point.new(3, 4)
line = Line.new(p1, p2)
puts line.to_s
ALine from #<Point:0x00007fffe30b8c20> to #<Point:0x00007fffe30b8c10>
BLine from 1, 2 to 3, 4
C(1, 2) to (3, 4)
DLine from (1, 2) to (3, 4)
Attempts:
2 left
💡 Hint

Each to_s returns a string, so nested calls build the full string.