From: Eleanor McHugh Date: 2008-05-29T13:28:40+09:00 Subject: Re: The duck's backside On 28 May 2008, at 21:09, Mark Wilden wrote: > On May 28, 2008, at 11:45 AM, Eleanor McHugh wrote: > >>> But if you want to check for numeric parameters, would it be >>> better just to use Numeric === v ? I know this isn't the ducky >>> way, but it certainly seems more "intentional." >> >> 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. > > Numeric === v is not a static typing convention (see http://en.wikipedia.org/wiki/Type_system) > . It's just OOP. It says in computer-understandable terms exactly > what you expressed in English: "v is numeric." No, it states that v inherits from the base class Numeric, and that is very much a static typing mindset, just projected onto an OO idiom. However Ruby is very much a dynamic language in which an instance of an object is not defined purely in terms of a chain of class inheritance, or even of mix-ins because an instance can have new methods directly attached to it. This being the case it makes much more sense to see if an instance responds to a given message of interest and then to act accordingly than to try and make assumptions based upon incomplete inheritance information. > I don't even know what the ability to respond to :to_int means. Does > that mean the entity is numeric? Does a numeric string qualify? Does > a floating point number qualify? Could Array support this method? > Does every class that responds to to_int do semantically equivalent > things? (think Cowboy#draw and Artist#draw). The only way to tell is > to test it or look it up. Yes. But as to_i, to_int, to_f, to_a, to_s and to_str are well known messages you can have a high confidence that when these are present in an instance they mean exactly what you expect them to mean. In practice this extends to a much larger vocabulary. > This doesn't make it "marginally" less readable than Numeric === v, > IMO. > > Different strokes, of course... Exactly. If you don't like Duck Typing don't use it. It's perfectly possible to design code in Ruby similarly to the way you would in Java or C++ if that's what you're comfortable with. Just don't be surprised if you find yourself writing a lot more code than is necessary. Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason