Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Legacy code and aliases to user input

1 view
Skip to first unread message

Marc Heiler

unread,
Jun 28, 2009, 10:21:28 AM6/28/09
to
I have a rather old code with a very long case/menu, which
acts on user-supplied input. I already fear that I will have
to rewrite it heavily, but before I start I thought I go and
ask here.

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/.

Joel VanderWerf

unread,
Jun 28, 2009, 1:16:58 PM6/28/09
to
Marc Heiler wrote:
> How to solve the above? Using a big hash which keeps all
> aliases and only leave a single entry per when line, like so?

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

0 new messages