Method to class difficulties

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

OK, found the problem myself.
The last line of testMenu is ‘myMenu’ but should be ‘myMenu.menu’.
Gerard’s post pointed me to ruby-for-beginners.rubymonstas.org where the practice showed me the way.
Thanks, Gerard

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs