A typical line of that case/menu looks like so:
when "foobar","foo","f"
do_something()
Obviously, I did want the user be able to type foobar, foo, or f
and have the same method or code invoked. Lateron I realized that
this was not optimal, one better approach would have been this:
alias_listing:
foo, f -> pointing to foobar, which then invokes do_something()
(The real scenario is a bit more complicated, but the code above
serves as a good trivial example.)
How to solve the above? Using a big hash which keeps all
aliases and only leave a single entry per when line, like so?
# process input here and check on aliases, and then replace
# them pointing to "foobar"?
when "foobar"
do_something
when "blafoo"
do_something_else
--
Posted via http://www.ruby-forum.com/.
Hashing is fine. Some alternatives:
Regex:
when /^f(oo(bar)?)?$/
Splat:
FOOBAR = %w{ f foo foobar }
..
when *FOOBAR
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407