Mark Probert <probertm / nortelnetworks.com> 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

>      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]
>          else
>              puts "--> unknown ref (#{ref})"
>          end
>      end
>    end

I wouldn't use eval, but rather send

          if @dispatch[ref]
              send @dispatch[ref]
          else

-- 
matt