From: Yossef Mendelssohn Date: 2012-01-15T16:22:27+09:00 Subject: Re: Case menu - obtain the first conditions? On Jan 15, 1:36 am, Marc Heiler wrote: > Hi. > > Consider I have a case menu like this. > > def enter_menu >   case @i >   when 'list_cars','lc' >     list_cars >   when 'list_horses','lh' >     list_horses >   when 'list_plants','lp >     list_plants >   when 'help' >     show_help >   end > end > > Now, inside the method show_help, I would like to > call the FIRST entry on each case menu. > > I.e: list_cars list_horses and list_plants > > Is there a way to do this easily? > > The actual case menu is very very long, with multiple aliases. > But the main name of every when entry is always the first entry. > > In other words I would need a way to programmatically access the > first option of every when clause. Don't use a case statement. You could use a hash, something like def menu_options options = { 'list_cars' => proc { list_cars }, 'lc' => 'list_cars', 'list_horses' => proc { list_horses }, 'lh' => 'list_horses', 'list_plants' => proc { list_plants }, 'lp' => 'list_plants', 'help' => proc { show_help } } options.default = proc { show_help } options end def enter_menu option = @i action = nil until action.respond_to?(:call) action = menu_options[option] option = action end action.call end def show_help options = menu_options main_options = options.keys.select { |k| options[k].respond_to? (:call) } puts "The real options are #{main_options.inspect}" end This could be much improved, but that's the general idea. -- -yossef