2014-01-12 32 views
6

私は次のエラーメッセージ得続ける:ので未定義のメソッド(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

あなたはRubyは宣言を前進できるようにすることができるはず高度な言語については、 –

+2

それを呼び出した後方法の選択肢を定義しています。 Objective-Cはそれを許します。 – Snowcrash

答えて

15

これは、次のとおりです。

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

をしかし、私は私の方法は「未定義」である理由を理解するように見えることはできません定義する前にメソッド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) 

私はwhileループを終了し、breakを使用。それ以外の場合、無限ループが作成されます。

+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 

このメソッドは、実行する前に呼び出す必要があります。ここではメインメソッドで定義をラップしますが、choices()の定義の後でのみmainを呼び出します。

0

App Academyの練習問題を解決するためにEclipseでRubyを実行しているのと同じエラーが発生していました。私は "オブジェクト"を追加するのを忘れていました。付属のテストケースに追加します。次の構文は動作します:

 #!/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("===============================================") 
関連する問題