From: Wyss Clemens Date: 2001-12-06T23:41:53+09:00 Subject: [ruby-talk:27685] RE: Emulating multimethods > I want to write a class that emulates a piece of hardware. The > hardware spec I'm working from says that the Hardware reacts to > different Inputs differently, depending on its State. if your state doesn't change on every call(which I hope for you ;-)) the "state-pattern" might help you here. At least to handle the "depending on its State"-problem: --------- class HardwareState def handle_input(input) ... end end class SleepingState < HardwareState def handle_input(input) ... super(input) end end .. class Hardware def initialize(initialStateName = 'Sleeping') switch_to_state(initialStateName) end def switch_to_state(stateName) @state = Kernel.const_get((stateName + 'State').intern()).new() end def handle_input(input) @state.handle_input(input) end end hw = Hardware.new('Sleeping') ... hw.handle_input(something) ... hw.handle_input(somethingElse) ... hw.switch_to_state('Happy') ... hw.handle_input(something) -------------- Regards Clemens