# Alternative to switch statements

**URL:** <https://www.ruby-forum.com/t/alternative-to-switch-statements/90180>\
**Category:** Ruby\
**Created:** [March 26, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/alternative-to-switch-statements/90180 "2007-03-26T18:06:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [March 26, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/alternative-to-switch-statements/90180/1 "2007-03-26T18:06:44Z")

</div>

Hi,

I am trying to write a ‘keyword-driven test automation framework’ in  
Ruby.

Using a case statement

def field(name, args) case name

when ‘username’  
name=‘login’

when ‘password’  
name=‘password’  
end

return $ie.text\_field(:name, name).set(args)

end

def button(name)  
case name

when ‘login’  
name = ‘Login’

```
end

return $ie.button(:value, name).click

```

end

Calling code

field 'username', 'removed\_email\_address@domain.invalid' field 'password', 'password123' button 'login'

I don’t like it because

1. It is procedural
2. Lengthy switch statements may affect performance

Could anyone suggest a way using, collections or symbols or something  
else?

Thanks

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [March 26, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/alternative-to-switch-statements/90180/2 "2007-03-26T18:15:45Z")

</div>

On 26.03.2007 18:04, aidy wrote:

> end
> 
> I don’t like it because
> 
> 1. It is procedural
> 2. Lengthy switch statements may affect performance
> 
> Could anyone suggest a way using, collections or symbols or something  
> else?

This is an obvious case for Hash with a custom default value handler:

MAP\_FIELD = Hash.new {|h,k| k}.update(  
‘username’ =\> ‘login’  
)

def field(name,args)  
$ie.text\_field(:name, MAP\_FIELD[name]).set(args)  
end

…

Kind regards

robert
