From: Alan Chen Date: 2002-02-20T04:28:15+09:00 Subject: Re: advice on dispatch tables On Wed, Feb 20, 2002 at 04:06:11AM +0900, Matt Armstrong wrote: > Mark Probert writes: > > > I am playing around with using a dispatch table rather > > than a huge case statement. I end up with code that > > looks something like this: > > > > > > class Bob > > > > def initialize > > @dispatch = Hash.new > > @dispatch['a'] = 'do_a' > > @dispatch['b'] = 'do_b' > > I'd use symbols instead of strings here: > > @dispatch['a'] = :do_a > @dispatch['b'] = :do_b Or you could use method objects which should run a bit faster. @dispatch['a'] = self.method( :do_a ) @dispatch['b'] = self.method( :do_b ) > > end > > > > def do_a > > puts "--> this is an a" > > end > > > > def do_b > > puts "--> this is an b" > > end > > > > def dispatch(ref=nil) > > if @dispatch[ref] > > eval @dispatch[ref] @dispatch[ref].call > > else > > puts "--> unknown ref (#{ref})" > > end > > end > > end -- Alan Chen Digikata LLC http://digikata.com