With some help from here I got the menu method working as a standalone but when I try to make it into a class I can’t seem to get the syntax right. Any pointers would be appreciated.
Thanks, Mike
def initialize(mHash)
@mHash = mHash
end
def menu ( mHash ) # =============================================
# def menu ( @mHash ) # ==================...
# /mc/bin/ruby/menu.rb:9: formal argument cannot be an instance variable
# given hash chose function to execute
# should be array of arrays, [ choice tag, string, function ]
# or hash of 2x array, { choice tag, [ string, function ] }
# draw the menu
@mHash.each_key { |k| puts k + ' ' + @mHash[k][0] }
puts 'choice? '
ch=gets.chomp
# puts 'ch=' + ch
if @mHash.has_key?( ch )
# puts 'mHash.has_key'
@mHash[ch][1].call
end
end
end
def testMenu
def aMethod
puts 'aMethod'
end
def bMethod
puts 'bMethod'
end
menuHash = {
'a' => [ 'aStr', method(:aMethod) ],
'b' => [ 'bStr', method(:bMethod) ],
}
myMenu = Menu.new(menuHash)
myMenu
end
testMenu