From: Guillaume Marcais Date: 2004-04-04T21:28:55+09:00 Subject: Re: deciding between ruby and python Le 4 avr. 04, � 05:37, Dan Doel a �crit : > Actually, in a sense, the automatic conversion is duck typing in this > case. > Defining #to_s in a Ruby class means, "You should call this to > automatically > convert if a String is expected." If you just want to be able to cast > to a > String, you define #to_str. This is not called for implicit > conversion, and is > more analogous to the __str__ method in Python, I think. The only > difference > is that Object has a default #to_s, so if you want it to raise an > exception, > you need to redefine it to do so. > > At least, that's how I've seen it described (and a quick check reveals > that > join calls #to_s, never #to_str, and when I redefine #to_s, it can > throw an > exception, just like Python would). I had the exact opposite understanding of the meaning of #to_s and #to_str. $ cat str.rb class Toto; end t = Toto.new begin puts("hello " + t) rescue Exception => e puts("Error " + e.message) end class Toto def to_str "toto" end end begin puts("hello " + t) rescue Exception => e puts("Error " + e.message) end $ ruby str.rb Error cannot convert Toto into String hello toto #join on the other hand calls #to_s on each element of the Array. Guillaume. >> Aren't you blowing this out of proportion? Would Python be on the >> 'wild side' if it added the following function: >> >> def join_as_strings(seq, sep): >> return sep.join([str(s) for s in seq]) >> >> See? Everyone can opt to choose the Ruby way in Python too, it's just >> not the default because it's deemed dangerous and a violation of >> "explicit is better than implicit". > > And the Ruby community doesn't always follow that principle, so our > default is the opposite. Neither is inherently better. > >> I think this thread has been an interesting example of how similar the >> two languages are - it started from "Python OO is a hack, Ruby is >> better in FP also because it supports List Processing" (someone still >> needs to tell me what List Processing is in Ruby) but it all really >> boils down to are these small cultural differences. And they have been >> hashed again and again in the past. > > I'm not the poster of that opinion, but I imagine he was referring to > things like #each and #inject and the Enumerable module. I don't > know Python, so I don't know what the analogues to these are, or > if it has them at all. Perhaps whenever he looked at Python, it > didn't. > > Ruby doesn't have list comprehensions like Python and Haskell do, > though, if that's what you took it to mean. Not having list > comprehensions doesn't mean your bad at list processing, though. > Otherwise Lisp would be bad at it (by default, at least), and Lisp > stands for List Processing. :) > >> I bet you could use either language, and get exactly the same >> productivity. > > I don't know that one is inherently better than the other at anything, > really. It's mostly a matter of taste. We should probably stop > quibbling. :) > > Have a nice day. > > - Dan >