Case menu - obtain the first conditions?

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.

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Jan 15, 1:36 am, Marc Heiler <sheve...@linuxmail.org> 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.

--
-yossef

Or a way to programmatically access your methods:

self.methods # => ["list_cars", "list_whatever", "list_plants", ...]

A better way would probably be: self.class.public_instance_methods(false)

···

On Jan 14, 2012, at 22:36 , Marc Heiler wrote:

In other words I would need a way to programmatically access the
first option of every when clause.

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

Hmm.

The problem is that I would be forced to use procs?

I don't like that.

I also feel as if the way to use aliases as in your example is VERY
cumbersome with hashes.

  when 'list_cars','lc'

Is much more readable.

Or a way to programmatically access your methods:

self.methods # => ["list_cars", "list_whatever", "list_plants", ...]

A better way would probably be:
self.class.public_instance_methods(false)

Hmm actually, you gave me a very good idea. I will probably toy a little
bit with that - I could follow with a convention of methods for
instance, or otherwise denote primary methods. Thanks Ryan!

By the way, also thanks to you Yossef Mendelssohn. Even if I initially
disliked your suggestion, I will continue to play with it and test it
slowly (which may help me to convince myself that it may not be so ugly
as it appears to be on first glance).

Thanks!

···

--
Posted via http://www.ruby-forum.com/\.