Method: HighLine::Menu#initialize

Defined in:
lib/highline/menu.rb

#initialize {|_self| ... } ⇒ Menu

Create an instance of HighLine::Menu. All customization is done through the passed block, which should call accessors, #choice and #choices as needed to define the Menu. Note that Menus are also Questions, so all that functionality is available to the block as well.

Examples:

Implicit menu creation through HighLine#choose

cli = HighLine.new
answer = cli.choose do |menu|
  menu.prompt = "Please choose your favorite programming language?  "
  menu.choice(:ruby) { say("Good choice!") }
  menu.choices(:python, :perl) { say("Not from around here, are you?") }
end

Yields:

  • (_self)

Yield Parameters:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/highline/menu.rb', line 52

def initialize
  #
  # Initialize Question objects with ignored values, we'll
  # adjust ours as needed.
  #
  super("Ignored", [], &nil) # avoiding passing the block along

  @items           = []
  @hidden_items    = []
  @help            = Hash.new("There's no help for that topic.")

  @index           = :number
  @index_suffix    = ". "
  @select_by       = :index_or_name
  @flow            = :rows
  @list_option     = nil
  @header          = nil
  @prompt          = "?  "
  @layout          = :list
  @shell           = false
  @nil_on_handled  = false

  # Used for coloring Menu indices.
  # Set it to default. But you may override it.
  @index_color     = self.class.index_color

  # Override Questions responses, we'll set our own.
  @responses       = {}
  # Context for action code.
  @highline        = nil

  yield self if block_given?

  init_help if @shell && !@help.empty?
end