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.
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.new(&details) .choices(*items) unless items.empty? # Set auto-completion .completion = . # Set _answer_type_ so we can double as the Question for ask(). # menu.option = normal menu selection, by index or name .answer_type = .shell ? shell_style_lambda() : . selected = ask() return unless selected if .shell if .gather selection = [] details = [] selected.each do |value| selection << value[0] details << value[1] end else selection, details = selected end else selection = selected end if .gather .gather_selected(self, selection, details) else .select(self, selection, details) end end |