undefined method (NoMethodError) ruby

后端 未结 3 1887
青春惊慌失措
青春惊慌失措 2020-12-18 01:24

I keep getting the following error message:

text.rb:2:in `
\': undefined method `choices\' for main:Object (NoMethodError)

But I

3条回答
  •  暖寄归人
    2020-12-18 01:56

    def main
    puts "Select [1] [2] [3] or [q] to quit"; users_choice = gets.chomp
    choices(users_choice)
    end
    
    def choices (choice)
      while choice != 'q'
        case choice
    
          when '1'
            puts "you chose one!"
            break
          when '2'
            puts "you chose two!"
            break
          when '3'
            puts "you chose three!"
            break
        end
      end
    end
    
    main
    

    The method only needs to be called prior to being executed. Here I wrap the definition in the main method, but only call main after the definition of choices().

提交回复
热议问题