On Thu, Aug 28, 2008 at 4:02 PM, Chris Bailey
<christopher.sean.bailey / gmail.com> wrote:
>  I'm trying to come up with an efficient way of using user input as a
> way of calling methods. I'm unhappy with the way that I am doing it
> because it isn't very flexible. This is what I'm doing now.
>
> input = gets.downcase.chomp
>
> if input == foo
>    do_foo()
> elsif input == bar
>    do_bar()
> else
>    puts "That isn't a command!"
> end
>
> What I would like to do is more like so.
>
> commands = {
>            'foo'   =>      do_foo(),
>            'bar'   =>      do_bar()
>           }


ACTIONS = %w[foo bar]

def execute(action)
  return send("do_#{action}") if ACTIONS.include?(action)
  raise "Unexpected action"
end




-- 
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com