2017-03-16 3 views
0

メソッド出力をクラス外から静的に呼び出す際に問題があります。具体的には、コードの最後にあるオブジェクトznumberのoutputメソッドを呼び出します。私はこのエラーを取得: 未定義のメソッドoutput' for #<WeightedScore:0x007ffb3815d840> (repl):58:in ' (REPL):9: `で'私はこのルビースクリプトをデバッグする助けを望んでいました。クラス外の出力関数を使用する際に問題があります

マイコード:

class WeightedScore 
 
    def initialize(scores) 
 
    @names = ['Amy', 'Annie', 'Fred', 'Marge', 'Tim', 'Sarah', 'John', 'Elise', 
 
     'Andy', 'Ellen'] 
 
    @emails = ['[email protected]', '[email protected]', '[email protected]', 
 
      '[email protected]', '[email protected]', '[email protected]', 
 
      '[email protected]', '[email protected]', '[email protected]', 
 
      '[email protected]'] 
 
    @scores = scores 
 
    end 
 

 
    def weighted_score(score) 
 
     if score == 0 
 
     return 0 
 
     else 
 
     return (215 + average_score)/score 
 
    end 
 

 
    def average_score 
 
    score = 0 
 
    i = 1 
 
    @scores.each do |score| 
 
     score += score 
 
     i = i + 1 
 
    end 
 
    return score/i 
 
    end 
 

 
    def output 
 
    i = 0 
 
    while i < 10 
 
     name = @names[i] 
 
     i = i + 1 
 
     line_string = i.to_s 
 
      line_string << ". #{@name[i]}, #{@emails[i]}, W = #{weighted_score(@scores[i]).to_s}" 
 
     if @scores[i] > 5 
 
      puts line_string 
 
     end 
 
    end 
 
    end 
 
end 
 
# START 
 

 
# these ten scores correspond, in order, with the names of test taker 
 
scores = [2, 5, -2, 9, 0, 23, -8, 7, 1, 4] 
 

 
# instantiate the class with the scores 
 
znumber = WeightedScore.new(scores) 
 
# print out the names, emails and weighted scores 
 
znumber.output 
 
end

は助けてくれてありがとう!

+1

「def self.output」と表示されます。 'def output'に変更するとどうなりますか? PNGで作業するのはかなり難しいです。あなたはコードを貼り付けて、58行目どこにあるのかを知らせ、あなた自身で問題を解決しようとしたことを説明する必要があります。 – pjd

+0

こんにちは!応答していただきありがとうございます!私はそれをしてもまだ動作しません。私はそれをインスタンスと静的メソッドに変更しようとしましたが、それでも動作しません。クラスのインスタンスで静的メソッドを呼び出そうとしているため、これを動作させる方法がわかりません。 –

+0

これで、コードを貼り付けてくれました。(ありがとう!)これはクラスメソッドと静的メソッドではなく、インスタンスメソッドです。このメソッドは、インスタンス化されたWeightedScoreオブジェクトに固有のインスタンス変数を使用していることに注意してください。 – pjd

答えて

0

いくつか変更がありましたが、出力が期待する内容が完全にわかりません。

最大の問題は、weighted_scoreメソッドでif-elseendで終了していないということでした。 endを入れてコードサンプルから最後のendを削除しました。 outputメソッドでは、@names[i]の代わりに@name[i]を参照しましたが、whileループのインクリメンタの位置は、意図した場所ではない可能性があります。

これが機能するかどうかを確認してください。私のために就寝。

class WeightedScore 
    def initialize(scores) 
    @names = ['Amy', 'Annie', 'Fred', 'Marge', 'Tim', 'Sarah', 'John', 'Elise', 
     'Andy', 'Ellen'] 
    @emails = ['[email protected]', '[email protected]', '[email protected]', 
      '[email protected]', '[email protected]', '[email protected]', 
      '[email protected]', '[email protected]', '[email protected]', 
      '[email protected]'] 
    @scores = scores 
    end 

    def weighted_score(score) 
     if score == 0 
     return 0 
     else 
     return (215 + average_score)/score 
     end 
    end 

    def average_score 
    score = 0 
    i = 1 
    @scores.each do |score| 
     score += score 
     i = i + 1 
    end 
    return score/i 
    end 

    def output 
    i = 0 
    while i < 10 
     name = @names[i] 
     line_string = i.to_s 
      line_string << ". #{@names[i]}, #{@emails[i]}, W = #{weighted_score(@scores[i]).to_s}" 
     if @scores[i] > 5 
      puts line_string 
     end 
     i = i + 1 
    end 
    end 
end 
0

"output"メソッドはクラスメソッドであり、 "znumber"はWeightedScoreインスタンス変数です。あなたは "自己"を削除する必要があります。 "def self.output"に出力をインスタンスメソッドにします。

+0

こんにちは、ありがとうございました!私はそれをやろうとしましたが、まだ動作させていません。 –

+0

Buddy、まず、weighted_scoreメソッドで "end"を指定してください。 第2に、 "@name"は出力メソッドの "@names"でなければなりません。 第3に、「(215 + average_score)/ score」に間違いがありますが、「score」を0にすることはできません。 最後に、@scores [i]がnilの場合、 "@scores [i]> 5"はエラーをトリガします。 – fanjieqi

+0

さらに、最後の行で「end」を削除します。 – fanjieqi

関連する問題