# Legacy code and aliases to user input

**URL:** <https://www.ruby-forum.com/t/legacy-code-and-aliases-to-user-input/171206>\
**Category:** Ruby\
**Created:** [June 28, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/legacy-code-and-aliases-to-user-input/171206 "2009-06-28T16:21:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 28, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/legacy-code-and-aliases-to-user-input/171206/1 "2009-06-28T16:21:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 28, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/legacy-code-and-aliases-to-user-input/171206/2 "2009-06-28T19:17:36Z")

</div>

Marc H. 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
