在 Ruby 中,以问号结尾的方法名称的真实性是惯用的吗?

发布于 2024-10-04 20:07:40 字数 358 浏览 1 评论 0原文

带问号的方法返回真实的内容(例如数字)以指示某事为真,这是否正常,还是应该返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撑一把青伞 2024-10-11 20:07:40

添加一个? 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.

塔塔猫 2024-10-11 20:07:40

?结尾的方法通常返回truefalse,但它不是系统性的,没有核心方法会假设它。

核心类中的一个示例是 Numeric#nonzero?,它从不返回 truefalse

42.nonzero? # => 42

Set 也有 add?delete? 。我希望 Enumerable#one? 返回 nilfalse 来区分计数为零和大于一的情况。

类似的示例是比较运算符(<>、...),通常仅返回 truefalse。这里,Module 的运算符再次存在例外,当两个模块不相关时,它们将返回 nil

Array > Enumerable  # => false
Array > Fixnum      # => nil

It is usual for methods ending with ? to return either true or false but it is not systematic and no core method will assume it.

An example in the core classes is Numeric#nonzero? which never returns true or false.

42.nonzero? # => 42

The library Set has add? and delete? too. I wish Enumerable#one? returned nil or false 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 only true or false. Here again exceptions exist for Module's operators that will return nil instead when the two modules are not related:

Array > Enumerable  # => false
Array > Fixnum      # => nil
只等公子 2024-10-11 20:07:40

您的问题有两个答案,并且都是有效的:

从技术上讲,任何返回 falsenil 值的内容在进行条件测试时都将充当 false 布尔值,就像非 nil 或 true 值充当 true。因此,在大多数情况下,当您想知道某个东西是否是整数时,所讨论的方法将正确工作。

但从风格上讲,这是一种以“?”结尾的方法。应该返回布尔值 truefalse 并且仅返回这些值。

所讨论的方法并没有很好地满足我们的期望,并且未满足 POLS(“最小意外原则”),因为您可以合理地期望返回一个布尔值,并得到一个整数或 nil。当代码因意外的 nil 或 Fixnum 值而失败时,这可能会导致代码中出现令人不快的意外。

因此,虽然这是一个有效的方法,但它不是一个好的方法,我会在代码审查中提出它。这导致了一个单独的讨论,即诸如此类的微妙事情如何增强或损害代码维护,但这是一个完全不同的讨论。

There are two answers to your question, and both are valid:

Technically, anything returning a false or nil value acts as a false boolean when doing a conditional test, just as a non-nil or true 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 or false 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.

二智少女猫性小仙女 2024-10-11 20:07:40

我重命名了相关方法以删除 ?,这对于要回答的问题实际上并不重要。扫描以 ?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 the add? and delete? methods on Set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文