From: Florian Gross Date: 2005-03-15T22:59:08+09:00 Subject: Re: how do you duck-type something to String, so String believes you? ES wrote: > Possibly a bit too clever. While I like the reversal of roles in this > fashion to facilitate the current implementation, I'm somewhat leery > of the concept. > > First of all, an *single* explicit method is used, not the representative > API of the class; for example in this instance, anything that defines > #to_s > could be considered to be valid input; sometimes more matching methods > might be required. It is considerably harder to construct the idiom this > way, so I can see the need for this compromise. I'm not sure I get you there -- if anything that implemented .to_s would be considered a String "5" == 5 would be true as well... > Secondly, the naming is confusing. #to_str? str is not a class and a very > similar name #to_s does something different. A better choice would use the > entire class name (lowercase would be fine) and clearly indicate that > it's > not necessarily being made into another class but used as an instance of > the other class; hence SomeClass#as_string or something similar like my > favourite SomeClass#quack_like_a_string. I also think that :as_string would be clearer, but I'm not sure if having to learn this is worth the hassle of breaking compatibility and perhaps causing more confusion than good.