class Menu
menu.rb ###
Allows you to easily create menus in your terminal programs. ###
Developed by Brett. (https://github.com/notronaldmcdonald) ###
Public Class Methods
new(title, opts = 3, selectMode = "int")
click to toggle source
# File lib/artsy/menu.rb, line 9 def initialize(title, opts = 3, selectMode = "int") # you can set title and no. of opts for your menu object @menutitle = title @optstrings = Array.new(opts) # create an array for each option @optids = Array.new(opts) maxopts = opts - 1 # for use in the loop for i in 0..maxopts # cycle through the number of options, generating a default string for each @optstrings[i] = "Option #{i}" @optids[i] = i + 1 end # end of loop @methodLoader = Linkage.new(@optids.count) # create an object, passing all necessary arguments to it end
Public Instance Methods
assign(option, mname)
click to toggle source
# File lib/artsy/menu.rb, line 48 def assign(option, mname) # a wrapper function to assign a method to an option trueOpt = option - 1 # do some math @methodLoader.bindMethod(trueOpt, mname) end
setOpt(index, value)
click to toggle source
# File lib/artsy/menu.rb, line 23 def setOpt(index, value) # set the value of an option index_real = index - 1 @optstrings[index_real] = "#{value}" end
show()
click to toggle source
# File lib/artsy/menu.rb, line 29 def show() # print menu puts "#{@menutitle}" puts "" for i in 1..@optids.count # print each option n = i - 1 # to access strings via index puts "#{i}) #{@optstrings[n]}" # print the option end puts "" puts "Enter your selection: " @choice = gets.chomp puts "You entered #{@choice}." # now, we'll run some logic to ensure the choice was in @optids, then we can run the user's code! @choice = Integer(@choice) # make sure it is int @choice = @choice - 1 # convert to real @methodLoader.run(@choice) end