----- Original Message ----- From: "you CAN teach an old dog ..." <itsme213 / hotmail.com> Newsgroups: comp.lang.ruby To: "ruby-talk ML" <ruby-talk / ruby-lang.org> Sent: Thursday, June 12, 2003 6:26 PM Subject: Confused about to_s in Ruby / irb > I wish to control the default display of my objects, and thought to_s > would be the right hook. But ruby (at least irb) seems to use my to_s > only if the class has no instance variables. Am I doing something > wrong? > > What other display-related hooks are there? I guess you know about p and puts, right? puts obj will call obj.to_s, but p obj will call obj.inspect instead. I guess irb is doing the equivalent of a p. There is also to_str, but the difference between it and to_s is a little subtle. The to_s method is for conversion -- pretty much anything can be converted to a string. The to_str method is used when an objects wants to "masquerade" as a string. One common difference is that the latter will typically preserve all information, whereas the former may only present enough to render the object in a printable form. (At least I think I've seen that distinction somewhere.) Hal