Ruby Koans 评分项目。未定义的方法“长度”对于 nil:NilClass
我对 Ruby 和一般编程都是全新的。我正在研究 Ruby Koans。在卡住之前我已经达到了 176/274。
这是“评分项目”,我需要编写一个方法来计算给定骰子的得分。
这可能不是您见过的最优雅的代码,但这是我想到的:
def score(dice)
tally = 0
tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
if dice.sort.to_s[/[1]+/].length >= 3
tally += 1000
end
tally = tally + (dice.sort.to_s[/[5]+/].length % 3) * 50
if dice.sort.to_s[/[5]+/].length >= 3
tally += 500
end
if dice.sort.to_s[/[2]+/].length >= 3
tally += 200
end
if dice.sort.to_s[/[3]+/].length >= 3
tally += 300
end
if dice.sort.to_s[/[4]+/].length >= 3
tally += 400
end
if dice.sort.to_s[/[6]+/].length >= 3
tally += 600
end
return tally
end
第一个测试是:score([]) 需要返回 0
当我运行它时,我得到“undefined method `length' for nil: NilClass”(引用的行是 .length 的第一个实例) 这告诉我“dice.sort.to_s[/[1]+/]”和“score([])”为零,但是当我在 irb>> 中运行它时它是 0。
什么给出?
I am BRAND new to Ruby, and programing in general. I'm working my way through the Ruby Koans. I've made it up to 176/274 before getting stuck.
It's the "Scoring Project" I need to write a method to calculate the score of a given dice roll.
This may not be the most elegant code you've ever seen but here's what I came up with:
def score(dice)
tally = 0
tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
if dice.sort.to_s[/[1]+/].length >= 3
tally += 1000
end
tally = tally + (dice.sort.to_s[/[5]+/].length % 3) * 50
if dice.sort.to_s[/[5]+/].length >= 3
tally += 500
end
if dice.sort.to_s[/[2]+/].length >= 3
tally += 200
end
if dice.sort.to_s[/[3]+/].length >= 3
tally += 300
end
if dice.sort.to_s[/[4]+/].length >= 3
tally += 400
end
if dice.sort.to_s[/[6]+/].length >= 3
tally += 600
end
return tally
end
The first test is: score([]) needs to return 0
When I run it I get "undefined method `length' for nil:NilClass" (the line referenced is the first instance of .length)
This tells me that "dice.sort.to_s[/[1]+/]" with "score([])" is nil, but when i run it in irb>> it is 0.
What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的。知道了。
非常抱歉,我说过运行“dice.sort.to_s[/[1]+/]”返回零而不是零,这一定是因为它有一些我不知道的存储值。当我运行“[].sort.to_s[/[1]+/]”时,它正确返回零。因此,我将每个 if 语句嵌套在一个检查中,以确保不存在 nil 值。
所有测试都通过了。
OK. Got it.
Huge apologies, I had said that running "dice.sort.to_s[/[1]+/]" was returning zero not nil, it must have been because it had some storred value i didn't know about. When i ran "[].sort.to_s[/[1]+/]" it returned nil properly. So I nested each of my if statements in a check to make sure there wasn't a nil value.
All test's pass.
您使用什么 Ruby 版本?我使用的是 1.9.2,我的 IRB 给了我同样的错误,因为如果没有匹配项,正则表达式将返回 nil。
由于
dice = []
是边界情况,您可以在代码的第一行添加对此的检查,以便返回 0。我今天做了 RubyKoans,尽管如此,我的代码并不比你的漂亮,也许它会对你有所帮助:
对于这个蹩脚的函数,我深表歉意,但它确实有效,所以它可能会让你了解如何解决这个问题特别的问题。
祝你好运!
What Ruby version are you using? I'm using 1.9.2 and my IRB gives me the same error since the regexp returns nil if there is no match.
Being
dice = []
a border case, you could add a check for that on the first lines of your code in order to return 0 then.I did the RubyKoans today and even though, my code is not prettier than yours, maybe it'll help you out a bit:
Sorry about the crappy function, but it does work, so it might give you an idea of how to solve that particular problem.
Good luck!