From: Michael Neumann Date: 2001-05-08T21:57:06+09:00 Subject: [ruby-talk:14864] Re: Polymorphic methods Benjamin J. Tilly wrote: > Yes. Two solutions. > > 1. Create a hash whose keys are the types of object you want, and > whose values are the subroutines you want. (You can create > anonymous subroutines with the "proc" method in the kernel.) > Then you can do this: > > def doIt (object) > $action[object.type][object] > end > > Feel free to add existence checks and a default case. Also > you may want to add logic to follow the inheritance hierarchy > in case someone tries to subclass a type you know about. This > is called "dispatching on a hash lookup". My solution is similar to yours, only that it uses method_missing: class StaticPoly def test_Fixnum(number) p "its a number" end def test_String(str) p "its a string" end def method_missing(id, *args) send(id.to_s + "_" + args[0].type.to_s, *args) end end obj = StaticPoly.new obj.test(3) # => its a number obj.test("ha") # => its a string If you introduce a naming scheme for the parameter-types, perhaps like JNI or the linker of C++ does you could use overloading where looking at more than one argument. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de