From: Paul Brannan Date: 2001-12-06T23:46:03+09:00 Subject: [ruby-talk:27686] Re: Emulating multimethods On Thu, Dec 06, 2001 at 01:52:06PM +0900, Christopher S. Kush wrote: > Now, in CLOS, I could write multimethods and the runtime would > just handle everything. But Ruby doesn't have that (yet (hint, > hint)), so I came up with the following: I'm not sure if this is quite what you are looking for, but you can easily get a different function called depending on what state you are in by using Proc and/or Method objects: class Foo STATES = { 1 => :foo1, 2 => :foo2, 3 => :foo3 } def change_state(new_state) @state = new_state @foo = method(STATES[new_state]) end def initialize; change_state(1); end def foo(*args, &block); @foo.call(*args, &block); end def foo1; puts "one"; change_state(2); end def foo2; puts "two"; change_state(3); end def foo3; puts "three"; end end f = Foo.new f.foo #=> one f.foo #=> two f.foo #=> three Paul