ruby 方法应该以 ? (问号)只返回一个布尔值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10527997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:06:46  来源:igfitidea点击:

Should a method ending in ? (question mark) return only a boolean?

ruby

提问by Ken W

I think it's just common sense and Ruby convention to do this but I have this method:

我认为这样做只是常识和 Ruby 约定,但我有这个方法:

def is_subscribed?(feed_url)
  Subscription.find_by_user_id_and_feed_id(self[ :id ], Feed.find_by_feed_url(feed_url))
end

The only confusion I'm getting is, this doesn't return boolean like I originally anticipated by putting the question mark on the end of the method name. I was under the impression that when evaluating an object as conditional it returns trueif not nil.

我得到的唯一困惑是,这不会像我最初预期的那样通过将问号放在方法名称的末尾返回布尔值。我的印象是,当评估一个对象作为条件时,它返回trueif not nil

Apparently I'm missing the point here and it's not evaluating it like I thought.

显然我在这里错过了这一点,它并没有像我想象的那样评估它。

So, my question is, would it be best to just do an if (condition) true else false? Or is there a more elegant method of doing this?

所以,我的问题是,最好只做一个if (condition) true else false?或者有没有更优雅的方法来做到这一点?

回答by Benjamin Udink ten Cate

A method ending with ? should return a value which can be evaluated to true or false. If you want to ensure a boolean return, you can do so by adding a double bang to the finder.

以 ? 结尾的方法 应该返回一个可以评估为真或假的值。如果要确保返回布尔值,可以通过向查找器添加双响来实现。

def is_subscribed?(feed_url)
  !!Subscription.find_by_user_id_and_feed_id(self[ :id ], Feed.find_by_feed_url(feed_url))
end

回答by Andrew Marshall

Adding a ?to the end of a method name does not in any way change the return value of the method, but instead only indicates that it is a predicate method. That is, that the method's return value should be treated as a boolean, but does not need to be strictly boolean (i.e. trueor false).

?方法名的末尾添加 a不会以任何方式改变方法的返回值,而只是表明它是一个谓词方法。也就是说,该方法的返回值应该被视为一个布尔值,但不需要是严格的布尔值(即truefalse)。

Many of the other answers state that it should return a value that is truthy or falsy. This is rather redundant, since everythingcan be either truthy or falsy, and since all methods in Ruby return something (unless they raise an exception), the return value is alwaystruthy or falsy.

许多其他答案指出它应该返回一个真值或假值。这是相当多余的,因为一切都可以是真或假,而且因为 Ruby 中的所有方法都会返回一些东西(除非它们引发异常),所以返回值总是真或假。

Think of appending a ?as a nicer alternative to prepending is_in other languages; e.g. I would either have subscribed?or is_subscribed.

将附加 a?视为is_在其他语言中附加的更好的替代方法;例如,我要么有subscribed?要么is_subscribed

回答by x1a4

It should a 'truthy' or 'falsy' value, that can be used safely in predicates, but does not have to return literal trueor false. There are even methods like this, like File.size?, in the standard library.

它应该是一个 'truthy' 或 'falsy' 值,可以在谓词中安全地使用,但不必返回文字truefalse. File.size?标准库中甚至还有这样的方法,比如。

回答by Kevin Bedell

Actually, to be specific -- methods ending in a question mark should return values that can be tested as trueor false.

实际上,具体来说——以问号结尾的方法应该返回可以作为true或测试的值false

There are many methods in rails that return non-boolean values from '?' methods.

rails 中有许多方法可以从 '?' 返回非布尔值。方法。

In fact there was recently a pull request submitted to the rails project that focussed attention on this exact matter:

事实上,最近有一个提交给 rails 项目的 pull request 将注意力集中在这个问题上:

https://github.com/rails/rails/pull/5582

https://github.com/rails/rails/pull/5582

Basically, the discussion was around this exact issue -- methods only need to return values that can be tested as true or false, like so:

基本上,讨论围绕着这个确切的问题——方法只需要返回可以被测试为真或假的值,如下所示:

if (condition)
  # do 'truthy option
else
  # do non-truthy option
end

From that perspective, I believe your method is fine.

从这个角度来看,我相信你的方法是好的。

回答by David Hempy

The other answers cover the return value very well.

其他答案很好地涵盖了返回值。

I'll add that modern style guides discourage the is_prefix to boolean methods. The trailing question mark covers that semantic sugar.

我要补充一点,现代风格指南不鼓励is_布尔方法的前缀。尾随的问号涵盖了语义糖。

From https://github.com/rubocop-hq/ruby-style-guide:

来自https://github.com/rubocop-hq/ruby-style-guide

Boolean Methods Question Mark

The names of predicate methods (methods that return a boolean value) should end in a question mark (i.e. Array#empty?). Methods that don't return a boolean, shouldn't end in a question mark.

Boolean Methods Prefix

Avoid prefixing predicate methods with the auxiliary verbs such as is, does, or can. These words are redundant and inconsistent with the style of boolean methods in the Ruby core library, such as empty? and include?.

布尔方法问号

谓词方法(返回布尔值的方法)的名称应以问号结尾(即 Array#empty?)。不返回布尔值的方法不应以问号结尾。

布尔方法前缀

避免在谓词方法前加上助动词,例如 is、dos 或 can。这些词是多余的,与Ruby核心库中布尔方法的风格不一致,比如空?并包括?。