2014-01-12 188 views
6

我不斷收到以下錯誤信息:未定義的方法(NoMethodError)紅寶石

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

但我似乎無法理解爲什麼我的方法是「不確定」:

puts "Select [1] [2] [3] or [q] to quit"; users_choice = gets.chomp 
choices(users_choice) 

def choices (choice)  
    while choice != 'q'  
     case choice 

     when '1' 
      puts "you chose one!" 

     when '2' 
      puts "you chose two!" 

     when '3' 
      puts "you chose three!" 
     end  
    end 
end 
+0

您在調用它之後定義了方法選擇 –

+2

對於高級語言,Ruby應該能夠允許前向聲明。 Objective-C允許它。 – Snowcrash

回答

15

這是因爲在定義之前,您正在調用方法choices。編寫代碼如下:

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) 

我以前break,從while循環退出。否則它會創建一個無限循環。

+0

當然,謝謝。 – stecd

+0

嗯,我認爲這不應該是原因,因爲在Ruby中調用它之前或調用之後定義變量沒有區別。 – ImranNaqvi

+1

@ImranNaqvi你確定嗎? –

3
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 

該方法只需要在執行前調用。在這裏,我將這個定義包裝在main方法中,但是隻在choose()的定義之後調用main。

0

我在Eclipse中運行Ruby時遇到了同樣的錯誤,正在執行App Academy練習練習。我忘了添加「對象」。到提供的測試用例。以下語法的工作原理如下:

 #!/usr/bin/ruby 
     class Prime 
     # Write a method that takes in an integer (greater than one) and 
     # returns true if it is prime; otherwise return false. 
     # 
     # You may want to use the `%` modulo operation. `5 % 2` returns the 
     # remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case 
     # of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`. 
     # More generally, if `m` and `n` are integers, `m % n == 0` if and only 
     # if `n` divides `m` evenly. 
     # 
     # You would not be expected to already know about modulo for the 
     # challenge. 
     # 
     # Difficulty: medium. 

     def primer(number) 
     if number < 2 
      return false 
     end 
     i = 10 
     while i > 1 
      if number > i && number != i 
      if number % i == 0 
       return false 
      end 
      end 
      i -= 1 
     end 
     return true 
     end 
    end 

     object = Prime. new 

     # These are tests to check that your code is working. After writing 
     # your solution, they should all print true. 

     puts("\nTests for #primer") 
     puts("===============================================") 
      puts('primer(2) == true: ' + (object.primer(2) == true).to_s) 
      puts('primer(3) == true: ' + (object.primer(3) == true).to_s) 
      puts('primer(4) == false: ' + (object.primer(4) == false).to_s) 
      puts('primer(9) == false: ' + (object.primer(9) == false).to_s) 
     puts("===============================================") 
相關問題