メソッド出力をクラス外から静的に呼び出す際に問題があります。具体的には、コードの最後にあるオブジェクト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
は助けてくれてありがとう!
「def self.output」と表示されます。 'def output'に変更するとどうなりますか? PNGで作業するのはかなり難しいです。あなたはコードを貼り付けて、58行目どこにあるのかを知らせ、あなた自身で問題を解決しようとしたことを説明する必要があります。 – pjd
こんにちは!応答していただきありがとうございます!私はそれをしてもまだ動作しません。私はそれをインスタンスと静的メソッドに変更しようとしましたが、それでも動作しません。クラスのインスタンスで静的メソッドを呼び出そうとしているため、これを動作させる方法がわかりません。 –
これで、コードを貼り付けてくれました。(ありがとう!)これはクラスメソッドと静的メソッドではなく、インスタンスメソッドです。このメソッドは、インスタンス化されたWeightedScoreオブジェクトに固有のインスタンス変数を使用していることに注意してください。 – pjd