Method: HighLine#choose

Defined in:
lib/highline.rb

#choose(*items, &details) ⇒ String

This method is HighLine’s menu handler. For simple usage, you can just pass all the menu items you wish to display. At that point, choose() will build and display a menu, walk the user through selection, and return their choice among the provided items. You might use this in a case statement for quick and dirty menus.

However, choose() is capable of much more. If provided, a block will be passed a HighLine::Menu object to configure. Using this method, you can customize all the details of menu handling from index display, to building a complete shell-like menuing system. See HighLine::Menu for all the methods it responds to.

Raises EOFError if input is exhausted.

Parameters:

  • items (Array<String>)
  • details (Proc)

    to be passed to Menu.new

Returns:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/highline.rb', line 252

def choose(*items, &details)
  menu = Menu.new(&details)
  menu.choices(*items) unless items.empty?

  # Set auto-completion
  menu.completion = menu.options

  # Set _answer_type_ so we can double as the Question for ask().
  # menu.option = normal menu selection, by index or name
  menu.answer_type = menu.shell ? shell_style_lambda(menu) : menu.options

  selected = ask(menu)
  return unless selected

  if menu.shell
    if menu.gather
      selection = []
      details = []
      selected.each do |value|
        selection << value[0]
        details << value[1]
      end
    else
      selection, details = selected
    end
  else
    selection = selected
  end

  if menu.gather
    menu.gather_selected(self, selection, details)
  else
    menu.select(self, selection, details)
  end
end