Bug #4453

Overriding #to_s changes #inspect

Added by Joey Zhou about 3 years ago. Updated over 1 year ago.

[ruby-core:<unknown>]
Status:Closed
Priority:Normal
Assignee:Yusuke Endoh
Category:-
Target version:-
ruby -v:ruby 1.9.2p180 (2011-02-18) [i386-mingw32] Backport:

Description

My Ruby is: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

sample codes 1:

class Foo # subclass of Object, inherits #inspect and #to_s
def initialize(bar,baz)
@bar, @baz = bar, baz
end
end

obj = Foo.new(:cat, :dog)

puts "\n== #inspect ====="
p obj #
puts obj.inspect #
printf "%p", obj #

puts "\n== #tos ========"
puts obj.to
s #Foo:0xb44398
printf "%s", obj #Foo:0xb44398

Yes, #inspect and #to_s are not synonyms, they return different strings.

sample codes 2, add 'def to_s':

class Foo
def initialize(bar,baz)
@bar, @baz = bar, baz
end
def tos # override #tos method
"has @bar = #{@bar}, @baz = #{@baz}."
end
end

obj = Foo.new(:cat, :dog)

puts "\n== #inspect ====="
p obj # has @bar = cat, @baz = dog.
puts obj.inspect # has @bar = cat, @baz = dog.
printf "%p", obj # has @bar = cat, @baz = dog.

puts "\n== #tos ========"
puts obj.to
s # has @bar = cat, @baz = dog.
printf "%s", obj # has @bar = cat, @baz = dog.

However, overriding #to_s makes #inspect do the identical thing. I don't think it's perfect.

In fact, the problem was reported in 2009. http://redmine.ruby-lang.org/issues/1786

At the bottom of the page, Matz said:

"Redefining #to_s should not affect inspect, if they are totally different."

I agree with Matz. #to_s and #inspcet should not be synonyms:

#to_s maybe for the user of the application, they want a readable message;

but #inspect maybe for the programmer, they want a debug information.

So, if this is a bug, maybe it should be fixed. If it's a feature in 1.9.2, I think it's not a good one,

because I lose a quick and convenient debug method to know an object's class the its instance variables.

I may want to show something readable to user of my app, as well as something usable for myself.

The feature (or bug) doesn't satisfy both.

to_s_and_inspect.txt Magnifier (2.07 KB) Joey Zhou, 03/01/2011 11:53 AM


Related issues

Related to ruby-trunk - Feature #1786: unexpected #inspect behaviour Closed 07/18/2009
Duplicated by ruby-trunk - Feature #6130: inspect using to_s is pain Closed 03/12/2012

History

#1 Updated by Joey Zhou about 3 years ago

=begin
My Ruby is: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

sample codes 1:
class Foo # subclass of Object, inherits #inspect and #to_s
def initialize(bar,baz)
@bar, @baz = bar, baz
end
end

obj = Foo.new(:cat, :dog)

puts "\n== #inspect ====="
p obj #
puts obj.inspect #
printf "%p", obj #

puts "\n== #tos ========"
puts obj.to
s #Foo:0xb44398
printf "%s", obj #Foo:0xb44398

Yes, #inspect and #to_s are not synonyms, they return different strings.

sample codes 2, add 'def tos':
class Foo
def initialize(bar,baz)
@bar, @baz = bar, baz
end
def to
s # override #to_s method
"has @bar = #{@bar}, @baz = #{@baz}."
end
end

obj = Foo.new(:cat, :dog)

puts "\n== #inspect ====="
p obj # has @bar = cat, @baz = dog.
puts obj.inspect # has @bar = cat, @baz = dog.
printf "%p", obj # has @bar = cat, @baz = dog.

puts "\n== #tos ========"
puts obj.to
s # has @bar = cat, @baz = dog.
printf "%s", obj # has @bar = cat, @baz = dog.

However, overriding #to_s makes #inspect do the identical thing. I don't think it's perfect.

In fact, the problem was reported in 2009. http://redmine.ruby-lang.org/issues/1786
At the bottom of the page, Matz said:
"Redefining #tos should not affect inspect, if they are totally different."
I agree with Matz. #to
s and #inspcet should not be synonyms:
#to_s maybe for the user of the application, they want a readable message;
but #inspect maybe for the programmer, they want a debug information.

So, if this is a bug, maybe it should be fixed. If it's a feature in 1.9.2, I think it's not a good one,
because I lose a quick and convenient debug method to know an object's class the its instance variables.
I may want to show something readable to user of my app, as well as something usable for myself.
The feature (or bug) doesn't satisfy both.

=end

#2 Updated by Joey Zhou about 3 years ago

=begin
I'm so sorry! I copy the report from my notepad. I don't know why the text formatting get so messy.
so I upload the report txt...
=end

#3 Updated by Yui NARUSE about 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

=begin

=end

#4 Updated by Koichi Sasada almost 3 years ago

Matz, how about it?

#5 Updated by salvatore giudice about 2 years ago

Is there some reason that this bug persists for years? Every time I'm debugging a new ruby programmer's code, I run into this. Rookies love to override tos for some odd reason. When inspect and pp break on tos, it makes me see flames and hear the screaming children again.

#6 Updated by Yui NARUSE about 2 years ago

qwerty55 (salvatore giudice) wrote:

Is there some reason that this bug persists for years?

Because you didn't report it.
You should thank yimutang.

#7 Updated by Yusuke Endoh about 2 years ago

  • Assignee changed from Yukihiro Matsumoto to Yusuke Endoh

A feature ticket #6130 duplicates this ticket.
And matz accepted #6130. So this will be "fixed" in 2.0

To prevent a discussion log from being scattered, please
don't reply to this ticket any more.

Yusuke Endoh mame@tsg.ne.jp

#8 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Assigned to Closed

"Fixed" at r36699. See #6130.

Yusuke Endoh mame@tsg.ne.jp

Also available in: Atom PDF