# Extending to an existing case menu

**URL:** <https://www.ruby-forum.com/t/extending-to-an-existing-case-menu/121137>\
**Category:** Ruby\
**Created:** [November 24, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/extending-to-an-existing-case-menu/121137 "2007-11-24T12:23:43Z")\
**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:** [November 24, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/extending-to-an-existing-case-menu/121137/1 "2007-11-24T12:23:43Z")

</div>

user\_input = $stdin.gets.chomp

case user\_input  
when ‘x’  
puts ‘this is a test’  
end

# So far so fine. Now I want to add something like

when ‘y’  
puts ‘yay, a new test’  
end

What would be the simplest or most elegant way?  
A hash with key -\> action ?

Only problem is that the action could be invoking a method or creating  
an object etc… and i dont want to use proc or lambda’s at all.

I would just like to “extend” the case menu somehow  
(I need to split a huge case menu into “core” modules, and  
stuff which a user wants to add to it)

---

<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:** [November 24, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/extending-to-an-existing-case-menu/121137/2 "2007-11-24T16:05:43Z")

</div>

On Nov 24, 4:23 am, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts ‘yay, a new test’  
> end
> 
> What would be the simplest or most elegant way?  
> A hash with key → action ?

The simplest, most elegant way, would be to just type it into the same  
case code 🙂

If you want to be able to define the (arbitrary) results of a  
comparison separate from the comparison itself, I think you need to  
use blocks/procs. A Hash would certainly be a good way to store this,  
assuming a simple case-like #=== comparison is all you need.

However, the syntax of defining a Hash of procs may not be the most  
ideal, depending on your scenario. I might use a method wrapper like  
so:

class ConditionMapper  
def initialize  
@conditions = {}  
end  
def when( \*conditions, &action )  
conditions.each{ |condition| @conditions[condition] = action }  
end  
def check( obj )  
@conditions.each{ |condition,action|  
if condition === obj  
return action[obj]  
end  
}  
end  
end

english\_match = ConditionMapper.new  
english\_match.when( 1…5 ){ |x|  
puts “#{x} is between one and five”  
}

english\_match.when( 7, 42 ){ |x|  
puts “#{x} is a cool number”  
}

english\_match.check( 4 )  
#=\> 4 is between one and five

english\_match.check( 42 )  
#=\> 42 is a cool number

> Only problem is that the action could be invoking a method or creating  
> an object etc… and i dont want to use proc or lambda’s at all.

Why not?
