[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Mthod redefinition

Austin Ziegler

9/12/2003 10:38:00 PM

Ferenc's code does, more or less, what Ryan Pavlik's StrongTyping module
does.

However, with the exception of nil testing, you may feel more comfortable
with duck typing.

For your simple case, it might be:

class Bar
def foo(arg = nil)
return "Who gives no input will get no output." if arg.nil?
return "One gives me #{arg}."
end
end

arg is, of course, not limited to integers at this point. The question would
then become what is it that you wish to do with integers that you need the
value?

Duck typing is based mostly on realising what sort of operations you want to
do with the object and testing for those operations, rather than testing for
the class. As Dave is fond of saying: type and class aren't the same.

-austin
--
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.09.12
* 18.29.32




3 Answers

Meino Christian Cramer

9/13/2003 12:44:00 AM

0

Gavin Sinclair

9/13/2003 1:26:00 AM

0

On Saturday, September 13, 2003, 10:44:02 AM, Meino wrote:

> From: Austin Ziegler <austin@halostatue.ca>
> Subject: Re: Mthod redefinition
> Date: Sat, 13 Sep 2003 07:38:09 +0900

> Hi

> Thank you for the explanation of duck typing !
> Now I have understood, what these little ducks have to do with ruby
> programming.

> Would it be correct if I would say: Duck typing is more or less
> equal to providing the code for calling the "same" method of
> different signatures be hand while C++ and other OOP-languages will
> do this automagically while compiling the code ?

There''s no magic really. In Ruby, you can only have one definition of
a method. I think it''s pretty ugly to have code inside a method
picking apart the arguments and performing different behaviours.
Sometimes it''s unavoidable, sometimes it''s OK, but mostly I try a
different approach.

Gavin


Jim Weirich

9/13/2003 2:35:00 AM

0

On Fri, 2003-09-12 at 20:44, Meino Christian Cramer wrote:
> Would it be correct if I would say: Duck typing is more or less
> equal to providing the code for calling the "same" method of
> different signatures be hand while C++ and other OOP-languages will
> do this automagically while compiling the code ?

I would disagree with this point of view. I think it misses the point
of duck typing. Duck typing has more to do with how polymorphism is
handled rather than method overloading is handled.

I started a separate thread to discuss it (see Duck Typing), so I won''t
repeat myself here.

--
-- Jim Weirich jweirich@one.net http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)