https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-03-01T11:47:25ZRuby Issue Tracking SystemRuby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=159802011-03-01T11:47:25Zyimutang (Joey Zhou)
<ul></ul><p>=begin<br>
My Ruby is: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]</p>
<p>sample codes 1:<br>
class Foo # subclass of Object, inherits #inspect and #to_s<br>
def initialize(bar,baz)<br>
@bar, @baz = bar, baz<br>
end<br>
end</p>
<p>obj = Foo.new(:cat, :dog)</p>
<p>puts "\n== #inspect ====="<br>
p obj #<Foo:0xb44398 @bar=:cat, @baz=:dog><br>
puts obj.inspect #<Foo:0xb44398 @bar=:cat, @baz=:dog><br>
printf "%p", obj #<Foo:0xb44398 @bar=:cat, @baz=:dog></p>
<p>puts "\n== #to_s ========"<br>
puts obj.to_s #<a href="Foo:0xb44398" class="external">Foo:0xb44398</a><br>
printf "%s", obj #<a href="Foo:0xb44398" class="external">Foo:0xb44398</a></p>
<p>Yes, #inspect and #to_s are not synonyms, they return different strings.</p>
<p>sample codes 2, add 'def to_s':<br>
class Foo<br>
def initialize(bar,baz)<br>
@bar, @baz = bar, baz<br>
end<br>
def to_s # override #to_s method<br>
"has @bar = #{@bar}, @baz = #{@baz}."<br>
end<br>
end</p>
<p>obj = Foo.new(:cat, :dog)</p>
<p>puts "\n== #inspect ====="<br>
p obj # has @bar = cat, @baz = dog.<br>
puts obj.inspect # has @bar = cat, @baz = dog.<br>
printf "%p", obj # has @bar = cat, @baz = dog.</p>
<p>puts "\n== #to_s ========"<br>
puts obj.to_s # has @bar = cat, @baz = dog.<br>
printf "%s", obj # has @bar = cat, @baz = dog.</p>
<p>However, overriding #to_s makes #inspect do the identical thing. I don't think it's perfect.</p>
<p>In fact, the problem was reported in 2009. <a href="http://redmine.ruby-lang.org/issues/1786" class="external">http://redmine.ruby-lang.org/issues/1786</a><br>
At the bottom of the page, Matz said:<br>
"Redefining #to_s should not affect inspect, if they are totally different."<br>
I agree with Matz. #to_s and #inspcet should not be synonyms:<br>
#to_s maybe for the user of the application, they want a readable message;<br>
but #inspect maybe for the programmer, they want a debug information.</p>
<p>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,<br>
because I lose a quick and convenient debug method to know an object's class the its instance variables.<br>
I may want to show something readable to user of my app, as well as something usable for myself.<br>
The feature (or bug) doesn't satisfy both.</p>
<p>=end</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=159812011-03-01T11:53:41Zyimutang (Joey Zhou)
<ul><li><strong>File</strong> <a href="/attachments/1512">to_s_and_inspect.txt</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/1512/to_s_and_inspect.txt">to_s_and_inspect.txt</a> added</li></ul><p>=begin<br>
I'm so sorry! I copy the report from my notepad. I don't know why the text formatting get so messy.<br>
so I upload the report txt...<br>
=end</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=160342011-03-06T05:26:48Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>=begin</p>
<p>=end</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=177452011-06-11T15:47:40Zko1 (Koichi Sasada)
<ul></ul><p>Matz, how about it?</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=255832012-04-02T10:55:09Zqwerty55 (salvatore giudice)toregiudice@gmail.com
<ul></ul><p>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 to_s for some odd reason. When inspect and pp break on to_s, it makes me see flames and hear the screaming children again.</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=255952012-04-02T19:05:00Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>qwerty55 (salvatore giudice) wrote:</p>
<blockquote>
<p>Is there some reason that this bug persists for years?</p>
</blockquote>
<p>Because you didn't report it.<br>
You should thank yimutang.</p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=255962012-04-02T21:25:34Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Assignee</strong> changed from <i>matz (Yukihiro Matsumoto)</i> to <i>mame (Yusuke Endoh)</i></li></ul><p>A feature ticket <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: inspect using to_s is pain (Closed)" href="https://bugs.ruby-lang.org/issues/6130">#6130</a> duplicates this ticket.<br>
And matz accepted <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: inspect using to_s is pain (Closed)" href="https://bugs.ruby-lang.org/issues/6130">#6130</a>. So this will be "fixed" in 2.0</p>
<p>To prevent a discussion log from being scattered, please<br>
don't reply to this ticket any more.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Bug #4453: Overriding #to_s changes #inspecthttps://bugs.ruby-lang.org/issues/4453?journal_id=331052012-11-20T02:33:54Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>"Fixed" at r36699. See <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: inspect using to_s is pain (Closed)" href="https://bugs.ruby-lang.org/issues/6130">#6130</a>.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p>