From: "David A. Black" Date: 2007-04-03T19:23:45+09:00 Subject: Re: New presentation on Ruby Hi -- On 4/3/07, Chauk-Mean P wrote: > 2007/4/2, Ilan Berci : > > >Slide 12) Doesn't illustrate duck typing at all as I can write the exact > > >same thing in Java. You could have illustrated the point more > > >accurately by assigning the types to a temporary and then invoking the > > >method > > >class Duck; def quack; "quack"; end; end; > > >class Bird; def quack; "chirp"; end; end; > > >[Duck.new, Bird.new].each {|duck| duck.quack} > > > As stated in the slide, "The type of an object is defined by what that > object can do (and not by its class/interface)." > > In the code snippet, > - I do not have to declare the type of a variable > a_duck = Duck.new > - I can request an object to walk and it will work if the object has the > corresponding method > a_duck.walk > As Duck defines a walk method, the above request will succeed and there is > no need for having a type defining the walk capability. > This is quite relevant regarding duck typing. I can see your point, but I think duck typing is better illustrated with examples that avoid the word "duck" in any form. The reason is that if you have a Duck class and some other class, and a_duck and a_fake_duck, you're basically saying that there's one class that *really* represents the type, and others that sort of emulate or imitate it. It's probably better to be even-handed about it: there are, say, two objects, and the "duck" abstraction applies equally to both. David -- Upcoming Rails training by Ruby Power and Light: Four-day Intro to Intermediate May 8-11, 2007 Edison, NJ http://www.rubypal.com/events/05082007