On Jan 15, 1:36=A0am, Marc Heiler <sheve... / linuxmail.org> wrote:
> Hi.
>
> Consider I have a case menu like this.
>
> def enter_menu
> =A0 case @i
> =A0 when 'list_cars','lc'
> =A0 =A0 list_cars
> =A0 when 'list_horses','lh'
> =A0 =A0 list_horses
> =A0 when 'list_plants','lp
> =A0 =A0 list_plants
> =A0 when 'help'
> =A0 =A0 show_help
> =A0 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 =3D {
      'list_cars' =3D> proc { list_cars }, 'lc' =3D> 'list_cars',
      'list_horses' =3D> proc { list_horses }, 'lh' =3D> 'list_horses',
      'list_plants' =3D> proc { list_plants }, 'lp' =3D> 'list_plants',
      'help' =3D> proc { show_help }
    }
    options.default =3D proc { show_help }
    options
  end

  def enter_menu
    option =3D @i
    action =3D nil

    until action.respond_to?(:call)
      action =3D menu_options[option]
      option =3D action
    end

    action.call
  end

  def show_help
    options =3D menu_options
    main_options =3D 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