From: Gavin Sinclair Date: 2004-04-05T09:08:01+09:00 Subject: Re: deciding between ruby and python On Monday, April 5, 2004, 8:44:59 AM, Dick wrote: > * Gavin Sinclair [0447 18:47]: >> On Monday, April 5, 2004, 12:09:58 AM, Yukihiro wrote: >> > Maybe "join" should call "to_str" instead of "to_s" in the future. >> Bad idea, IMO. "join" is designed for returning a string. It >> therefore has the right to create strings as necessary for this >> purpose. >> "join" is similar to "puts" in this regard. > Yeah, but it should be the objects decision whether it's able to provide a meaningful > String version of itself. I'm probably covering the same ground as DAB here, but there's a big difference between a String *version* of itself and a String *representation* of itself. #to_str is aimed at the former, and #to_s is aimed at the latter. I believe that #join is interested in String *representations*, not String *versions*, of objects. And if you, as the high-level programmer, want a different representation, there's always puts objects.map { |o| "... #{o.attribute} ..." }.join(", ") In this light, think of puts objects.join(", ") as using the **default string representation** of the objects. > I generally use to_s for debugging, or any situation when I need > to quickly 'see' the object - i.e. to_s provides a string > representation of itself for *my* benefit. I think you should be using #inspect here. That's intended for debugging, #to_s is not. There's definitely a reason for having both methods, and I think that's it. > So 99% of my classes have a to_s method, but it's typically a > list of its instance variables, for the very reason that puts calls it. > join or anything else that wants a string to work with should > ask the object to provide one, and to_str is there for that. > If I haven't implemented that method I'm saying > "I'm not a String, and I'm not going to pretend to be one". That's not consistent with my understanding of the intentions of #to_s and #to_str. These methods (and #to_i/#to_int, etc.) have been a reliable source of confusion for quite a while now. I hope that they get addressed in Pickaxe II or another educational resource. Cheers, Gavin