在 Ruby 中,以问号结尾的方法名称的真实性是惯用的吗?
带问号的方法返回真实的内容(例如数字)以指示某事为真,这是否正常,还是应该返回 true
本身?
例如,Ruby 标准库或 Rails 是否有使用真实性的示例?
背景:有人在 一个单独问题的答案,它返回一个整数表示 true,nil
表示 false。另一位用户对没有返回布尔值感到惊讶。
Is it normal for methods with a question mark to return something that's truthy (for example, a number) to indicate that something is true, or should true
itself be returned?
Are there any examples of truthiness being used in the Ruby standard library or by Rails, for example?
Background: Someone wrote a String#int?
method in an answer to a separate question, which returned an integer to represent true, and nil
to represent false. Another user was surprised at not returning a boolean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加一个? Ruby 中的方法名称是惯用的,该方法将返回 true 或 false。 Object#nil? 就是一个很好的例子。事实上,Object 有很多真实性检查的好例子。
Adding a ? to a method name in Ruby is idiomatic that the method will return true or false. Object#nil? is a good example. In fact, Object has a lot of good examples of truthiness checks.
以
?
结尾的方法通常返回true
或false
,但它不是系统性的,没有核心方法会假设它。核心类中的一个示例是
Numeric#nonzero?
,它从不返回true
或false
。库
Set
也有add?
和delete?
。我希望Enumerable#one?
返回nil
或false
来区分计数为零和大于一的情况。类似的示例是比较运算符(
<
、>
、...),通常仅返回true
或false
。这里,Module
的运算符再次存在例外,当两个模块不相关时,它们将返回nil
:It is usual for methods ending with
?
to return eithertrue
orfalse
but it is not systematic and no core method will assume it.An example in the core classes is
Numeric#nonzero?
which never returnstrue
orfalse
.The library
Set
hasadd?
anddelete?
too. I wishEnumerable#one?
returnednil
orfalse
to distinguish cases where the count is zero from when it is greater than one.A similar example are the comparison operators (
<
,>
, ...), which usually return onlytrue
orfalse
. Here again exceptions exist forModule
's operators that will returnnil
instead when the two modules are not related:您的问题有两个答案,并且都是有效的:
从技术上讲,任何返回
false
或nil
值的内容在进行条件测试时都将充当 false 布尔值,就像非 nil 或true
值充当 true。因此,在大多数情况下,当您想知道某个东西是否是整数时,所讨论的方法将正确工作。但从风格上讲,这是一种以“?”结尾的方法。应该返回布尔值
true
或false
并且仅返回这些值。所讨论的方法并没有很好地满足我们的期望,并且未满足 POLS(“最小意外原则”),因为您可以合理地期望返回一个布尔值,并得到一个整数或 nil。当代码因意外的
nil
或 Fixnum 值而失败时,这可能会导致代码中出现令人不快的意外。因此,虽然这是一个有效的方法,但它不是一个好的方法,我会在代码审查中提出它。这导致了一个单独的讨论,即诸如此类的微妙事情如何增强或损害代码维护,但这是一个完全不同的讨论。
There are two answers to your question, and both are valid:
Technically, anything returning a
false
ornil
value acts as a false boolean when doing a conditional test, just as a non-nil ortrue
value acts as a true. So, the method in question will work correctly for most times you'd want to know if something is an integer.But stylistically a method that ends with '?' should return either a Boolean
true
orfalse
and only those.The method in question doesn't really play nicely with our expectations and fails the POLS ("principle of least surprise") because you can reasonably expect a Boolean value being returned, and get an integer or a nil. THAT could lead to unpleasant surprises in code when it fails with an unexpected
nil
or a Fixnum value.So, while it's a valid method, it's not a good method, and I would have brought it up in a code review. And that leads to a separate discussion of how subtle things like that can enhance or hurt code maintenance, but that's an entirely different discussion.
我重命名了相关方法以删除
?
,这对于要回答的问题实际上并不重要。扫描以 ?s 结尾的核心函数,我发现唯一返回数据或 nil 的是 Set 上的add?
和delete?
方法。I renamed the method in question to remove the
?
, which was really not important to the question being answered. Scanning through the core functions that end in ?s, the only ones I spotted that returned data or nil were theadd?
anddelete?
methods on Set.