From: David Masover Date: 2008-05-29T05:44:36+09:00 Subject: Re: The duck's backside On Wednesday 28 May 2008 15:09:23 Mark Wilden wrote: > On May 28, 2008, at 11:45 AM, Eleanor McHugh wrote: > > That'd be marginally more readable but to my mind it's overly > > cautious: what's the point of using Ruby if you're still going to > > follow static typing conventions? > > The only point of using Ruby (or any other tool in the universe) is to > get a job done. :) I would never use a Ruby feature just because Ruby > makes it possible. Put it this way: What's the point of using, say, Erlang, if you never use its concurrency features? Granted, Ruby has a bit more going for it, but a lot of the joy of using Ruby is how much less work you end up having to do when you don't have to think about types too much. > . It's just OOP. It says in computer-understandable terms exactly what > you expressed in English: "v is numeric." No, it says "v is of class Numeric." It's very explicit, and makes the assumption that anything which acts like a number will eventually inherit from Numeric. > I don't even know what the ability to respond to :to_int means. I think it would be :to_i, actually... And I'd probably just call foo.to_i, rather than testing for the existence of to_i. Another possibility is: v.integer? > Does > that mean the entity is numeric? In a sense. > Does a numeric string qualify? Yes, it'll be parsed. > Does a > floating point number qualify? Yes, it'll be rounded down. > Could Array support this method? Anything could. Open classes mean you could add one to Array. But out of the box, it doesn't appear to. > Does > every class that responds to to_int do semantically equivalent things? Probably -- to_i is very much baked into the language, along with to_s and friends. Remember the above -- I could actually completely redefine Array, or Numeric, etc. So even your assumption that "Numeric === foo" tests for Numeric is really only based on convention -- you're assuming that no one, anywhere in your code, is doing stuff like this: Numeric = nil > (think Cowboy#draw and Artist#draw). Yes, that is a downside of duck typing, as currently implemented, but doesn't really apply to :to_i. Also, context matters. If Cowboy and Artist are both in a GUI widget library, that Cowboy is asking for trouble. And it's worth asking how the Cowboy came to be in an art studio, or how the Artist came to be in a saloon -- there are likely going to be more elegant solutions than if Cowboy === bill ...