[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 6:21:00 PM

You could use the StrongTyping module if you really need it.

Do you actually need to, though? Consider duck typing instead.

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




2 Answers

Meino Christian Cramer

9/12/2003 7:04:00 PM

0

Ferenc Engard

9/12/2003 8:33:00 PM

0

Hello,

This is something you want:

------------------------------
class Foo
def aMethod(param)
# Based on param''s type, call the appropriate function
if param.kind_of?(String)
return aMethod_string(param)
end
if param.kind_of?(Integer)
return aMethod_integer(param)
end
end

def aMethod_string(param)
# ...
end
def aMethod_integer(param)
# ...
end
private :aMethod_string, :aMethod_integer
end
------------------------------

OTOH, the ducktyping is just you have said (if I correctly understand):
if it can quak, it''s a duck, if it can speak, it''s a human. You can
inspect what an object can do with the respond_to?() method:

------------------------------
class Foo
def aMethod(param)
# Based on param''s capabilities,
# call the appropriate function
if param.respond_to?(:quak)
return aMethod_quaks(param)
end
if param.respond_to?(:speak)
return aMethod_speaks(param)
end
end

# It can quak, who cares what this param really is?
def aMethod_quaks(param)
# ...
end
def aMethod_speaks(param)
# ...
end
private :aMethod_quaks, :aMethod_speaks
end
------------------------------

Both are untested code, sorry if something do not work.

Regards:
Circum