Project

General

Profile

Bug #12174

Interpolation ignores `to_s` (and `inspect`) when `to_s` is defined to `nil`.

Added by sawa (Tsuyoshi Sawada) about 1 year ago. Updated about 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:74301]

Description

When to_s is defined to return nil, interpolation prints the original inspection.

class A
  def to_s; end
end

puts "#{A.new}" # => #<A:0x007f4edf19d720>

It even ignores an overwritten inspect definition.

class A
  def to_s; end
  def inspect; "foo" end
end

puts "#{A.new}" # => #<A:0x007f8176c09050>

History

#1 [ruby-core:74309] Updated by sawa (Tsuyoshi Sawada) about 1 year ago

The description was too specific. Whenever to_s is defined to be not a string, the issue arises.

#2 [ruby-core:74318] Updated by nobu (Nobuyoshi Nakada) about 1 year ago

  • Status changed from Open to Rejected

Yes, to be interpolated into a string, to_s must return a string.
Non-string object cannot be concatenated to a string.

Also available in: Atom PDF