[#35446] [Ruby 1.9 - Bug #4477][Open] Kernel:exec and backtick (`) don't work for certain system commands — Joachim Wuttke <j.wuttke@...>

10 messages 2011/03/07

[#35476] [Ruby 1.9 - Bug #4489][Open] [PATCH] Encodings with /-(unix|dos|mac)\Z/ — "James M. Lawrence" <quixoticsycophant@...>

20 messages 2011/03/10

[#35552] [Ruby 1.9 - Feature #4523][Open] Kernel#require to return the path of the loaded file — Alex Young <alex@...>

14 messages 2011/03/24

[#35565] [Ruby 1.9 - Feature #4531][Open] [PATCH 0/7] use poll() instead of select() in certain cases — Eric Wong <normalperson@...>

33 messages 2011/03/28

[#35566] [Ruby 1.9 - Feature #4532][Open] [PATCH] add IO#pread and IO#pwrite methods — Eric Wong <normalperson@...>

12 messages 2011/03/28

[#35586] [Ruby 1.9 - Feature #4538][Open] [PATCH (cleanup)] avoid unnecessary select() calls before doing I/O — Eric Wong <normalperson@...>

9 messages 2011/03/29

[ruby-core:35433] [Ruby 1.9 - Bug #4453][Assigned] Overriding #to_s changes #inspect

From: Yui NARUSE <redmine@...>
Date: 2011-03-05 20:28:33 UTC
List: ruby-core #35433
Issue #4453 has been updated by Yui NARUSE.

Status changed from Open to Assigned
Assignee set to Yukihiro Matsumoto


----------------------------------------
Bug #4453: Overriding #to_s changes #inspect
http://redmine.ruby-lang.org/issues/4453

Author: Joey Zhou
Status: Assigned
Priority: Normal
Assignee: 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://redmine.ruby-lang.org

In This Thread

Prev Next