我希望在调试这个 ruby 脚本时得到一些帮助。我在 class 外部使用输出函数时遇到问题
I was hoping for some help debugging this ruby script. I'm having trouble using the output function from outside the class
我在从 class 外部静态调用方法输出时遇到问题。具体调用代码末尾对象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 = ['amy@example.com', 'annie@example.com', 'fred@example.com',
'marge@example.com', 'tim@example.com', 'sarah@example.com',
'john@example.com', 'elise@example.com', 'andy@example.com',
'ellen@example.com']
@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
感谢您的帮助!
"output"方法是class方法,"znumber"是WeightedScore实例变量。您应该删除 "def self.output" 中的 "self." 以使输出成为实例方法。
我改变了一些东西,但我不完全确定你对输出的期望。
我认为最大的问题是在 weighted_score
方法中你没有以 end
结束你的 if-else
。我在那里放了一个 end
并从你的代码示例中删除了最后的 end
。在 output
方法中,您引用了 @name[i]
而不是 @names[i]
,并且您的 while 循环的增量器的位置可能不在预期的位置。
看看这是否适合您。我的就寝时间。
class WeightedScore
def initialize(scores)
@names = ['Amy', 'Annie', 'Fred', 'Marge', 'Tim', 'Sarah', 'John', 'Elise',
'Andy', 'Ellen']
@emails = ['amy@example.com', 'annie@example.com', 'fred@example.com',
'marge@example.com', 'tim@example.com', 'sarah@example.com',
'john@example.com', 'elise@example.com', 'andy@example.com',
'ellen@example.com']
@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
我在从 class 外部静态调用方法输出时遇到问题。具体调用代码末尾对象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 = ['amy@example.com', 'annie@example.com', 'fred@example.com',
'marge@example.com', 'tim@example.com', 'sarah@example.com',
'john@example.com', 'elise@example.com', 'andy@example.com',
'ellen@example.com']
@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
感谢您的帮助!
"output"方法是class方法,"znumber"是WeightedScore实例变量。您应该删除 "def self.output" 中的 "self." 以使输出成为实例方法。
我改变了一些东西,但我不完全确定你对输出的期望。
我认为最大的问题是在 weighted_score
方法中你没有以 end
结束你的 if-else
。我在那里放了一个 end
并从你的代码示例中删除了最后的 end
。在 output
方法中,您引用了 @name[i]
而不是 @names[i]
,并且您的 while 循环的增量器的位置可能不在预期的位置。
看看这是否适合您。我的就寝时间。
class WeightedScore
def initialize(scores)
@names = ['Amy', 'Annie', 'Fred', 'Marge', 'Tim', 'Sarah', 'John', 'Elise',
'Andy', 'Ellen']
@emails = ['amy@example.com', 'annie@example.com', 'fred@example.com',
'marge@example.com', 'tim@example.com', 'sarah@example.com',
'john@example.com', 'elise@example.com', 'andy@example.com',
'ellen@example.com']
@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