From: Derek Wyatt Date: 2005-08-27T00:43:29+09:00 Subject: Re: to_s and to_str -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Mark Volkmann wrote: | Here's what I've learned so far. | 1) Every object has to_s because Object provides a default implementation. | 2) Some classes override to_s to return a more useful String. | 3) to_str should only be implemented in classes whose objects can | logically be used as Strings. | | If you're going to implement to_str in a class, is there a reason why | it might return something different than to_s? Is one considered a | human-readable representation and the other something else? | | Isn't "can be logically used as Strings" a somewhat subjective thing? | class MyInteger ~ def to_s ~ "MyInteger has this value: #{@value}" ~ end ~ def to_str ~ "#{@value}" ~ end end I would say that to_str is more of a programmatically readable type of string... think of it more as a type conversion from the object's native value and the String value, whereas to_s is something more "pretty". Regs, D - -- Derek Wyatt - C++ / Ruby / Unix Programmer http://derekwyatt.org -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD8DBQFDDzicwwHFeC88e2IRAspEAKDRLLssxpiYZk/kyX8BZs3t18MkdwCg1XjM SBoAEkRxlMTx/G6QO3GSBuw= =GGs7 -----END PGP SIGNATURE-----