undefined method (NoMethodError) ruby

后端 未结 3 1891
青春惊慌失措
青春惊慌失措 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:54

    This is because you are calling method choices, before defining it. Write the code as below:

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

    I used break, to exit from the while loop. Otherwise it will create an infinite loop.

提交回复
热议问题