Ruby 方式:捕获除零
我有以下方法来计算平均值:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
这没什么特别的,但它有一个我期望所有平均方程都有的问题:如果输入全部为零,它可能会被零除。
所以,我想到这样做:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
if total==0
average = 0.00
else
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
end
......这有效,但对我来说感觉很笨拙。是否有更优雅的“Ruby Way”来避免这种被零除的问题?
我希望我有一个“除非那么”运算符,比如......
average = numerator / denominator unless denominator == 0 then 0
有什么建议吗?
I have the following method to compute an average:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
It's nothing special, but it has a problem that I expect all average equations have: it might divide by zero if inputs are all zero.
So, I thought of doing this:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
if total==0
average = 0.00
else
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
end
... and that works, but it feels kludgy to me. Is there a more elegant, "Ruby Way" to avoid this division by zero problem?
What I'm wishing I had was an "unless then" operator, like...
average = numerator / denominator unless denominator == 0 then 0
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用
nonzero?
,如下所示:更多人会更熟悉使用三元运算符
(total == 0 ? 1 :total)
,所以这是另一种可能性。You can use
nonzero?
, as in:More people would be more familiar with using the ternary operator
(total == 0 ? 1 : total)
, so that's another possibility.虽然这是一个过时的线程,但我想我会用一个简单的衬里来插话,你可以使用......
While this is an outdated thread I thought I would chime in with a simple one liner you can use...
通常依靠
rescue
来捕获异常,然后返回默认值:另外我会写:
as
It's common to rely on
rescue
to capture the exception, then to return the default value:Also I'd write:
as
对我来说,最干净的方法是:
它还可以让您免于处理
0 / 0
。正如@Andrew 指出的,这仅对整数有效。有关更多信息,请参阅此答案的评论。
To me, the cleanest way is:
It also saves you from handling
0 / 0
.As @Andrew points out, this is only valid for integers. See the comments to this answer for more info.
TL;DR:一种可能的解决方案
这可以防止所有情况:
原始功能:
考虑检查零:
此更改仅防止一种情况:
考虑使用内联
救援
此更改仅防止一种情况,也:
使用内联
rescue
还有另一个后果。考虑这个拼写错误:考虑使用
rescue
这更好,因为它不会隐藏错误,但可以防止与上面的倾斜
rescue
相同的情况。另一个版本,我称之为正常平均计算
作为旁注,我熟悉的平均操作是使用总计/计数来计算的,所以这里是一个执行此操作的版本。
这可以防止所有情况:
TL;DR: One possible solution
This protects against all cases:
Original function:
Consider checking for zero:
This change only protects against one case:
Consider using an inline
rescue
This change only protects against one case, also:
Using an inline
rescue
has another consequence. Consider this typo:Consider using a
rescue
This is better, as it does not hide errors, but protects against the same scenario as the incline
rescue
above.Another version with what I would call a normal average calculation
As a side note, the average operation I am familiar with is calculated using total/count, so here is a version that does that.
This protects against all cases:
我不太喜欢 Ruby,但我会这样做:
可能有更好的答案,但这可能就足够了。
I'm not much of a Ruby-ist, but I'd do it like this:
There is probably a better answer, but this might suffice.
如果要除的数字或分母是浮点数,则
/
不会返回零除错误。更一般地说,在 ruby1.9 下,
/
does not return a zero division error if either the number to be divided or the denominator is a float.More generally, under ruby1.9,