[#44036] [ruby-trunk - Feature #6242][Open] Ruby should support lists — "shugo (Shugo Maeda)" <redmine@...>

20 messages 2012/04/01

[#44084] [ruby-trunk - Bug #6246][Open] 1.9.3-p125 intermittent segfault — "jshow (Jodi Showers)" <jodi@...>

22 messages 2012/04/02

[#44156] [ruby-trunk - Feature #6265][Open] Remove 'useless' 'concatenation' syntax — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

45 messages 2012/04/06

[#44163] [ruby-trunk - Bug #6266][Open] encoding related exception with recent integrated psych — "jonforums (Jon Forums)" <redmine@...>

10 messages 2012/04/06

[#44303] [ruby-trunk - Feature #6284][Open] Add composition for procs — "pabloh (Pablo Herrero)" <pablodherrero@...>

57 messages 2012/04/12

[#44349] [ruby-trunk - Feature #6293][Open] new queue / blocking queues — "tenderlovemaking (Aaron Patterson)" <aaron@...>

10 messages 2012/04/13

[#44402] [ruby-trunk - Feature #6308][Open] Eliminate delegation from WeakRef — "headius (Charles Nutter)" <headius@...>

20 messages 2012/04/17

[#44403] [ruby-trunk - Feature #6309][Open] Add a reference queue for weak references — "headius (Charles Nutter)" <headius@...>

15 messages 2012/04/17

[#44533] [ruby-trunk - Bug #6341][Open] SIGSEGV: Thread.new { fork { GC.start } }.join — "rudolf (r stu3)" <redmine@...>

24 messages 2012/04/22

[#44630] [ruby-trunk - Feature #6361][Open] Bitwise string operations — "MartinBosslet (Martin Bosslet)" <Martin.Bosslet@...>

31 messages 2012/04/26

[#44648] [ruby-trunk - Feature #6367][Open] #same? for Enumerable — "prijutme4ty (Ilya Vorontsov)" <prijutme4ty@...>

16 messages 2012/04/26

[#44704] [ruby-trunk - Feature #6373][Open] public #self — "trans (Thomas Sawyer)" <transfire@...>

61 messages 2012/04/27

[#44748] [ruby-trunk - Feature #6376][Open] Feature lookup and checking if feature is loaded — "trans (Thomas Sawyer)" <transfire@...>

13 messages 2012/04/28

[ruby-core:44065] [ruby-trunk - Bug #4453] Overriding #to_s changes #inspect

From: "naruse (Yui NARUSE)" <naruse@...>
Date: 2012-04-02 10:05:01 UTC
List: ruby-core #44065
Issue #4453 has been updated by naruse (Yui NARUSE).


qwerty55 (salvatore giudice) wrote:
> Is there some reason that this bug persists for years?

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


----------------------------------------
Bug #4453: Overriding #to_s changes #inspect
https://bugs.ruby-lang.org/issues/4453#change-25595

Author: yimutang (Joey Zhou)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 
ruby -v: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]


# 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                 #<Foo:0xb44398 @bar=:cat, @baz=:dog>
puts obj.inspect      #<Foo:0xb44398 @bar=:cat, @baz=:dog>
printf "%p", obj      #<Foo:0xb44398 @bar=:cat, @baz=:dog>

puts "\n== #to_s ========"
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 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== #to_s ========"
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.



-- 
http://bugs.ruby-lang.org/

In This Thread

Prev Next