8 ms·
> this would never pass a code review, the expectation of "foo?" is that it returns true/false. 95% of the time, yes. However, the "official" rule is simply th
by tom-lord 11y ago
> this would never pass a code review, the expectation of "foo?" is that it returns true/false.
95% of the time, yes. However, the "official" rule is simply that such methods must return a value that can be EVALUATED as true/false. There are plenty of methods in the ruby core/standard library which end in a "?" but do not return true/false, such as:
* File#size (http://ruby-doc.org/core-2.2.3/File.html#method-c-size-3F http://ruby-doc.org/core-2.2.3/File.html#method-c-size-3F) returns Integer or nil.
* Integer#nonzero? (http://ruby-doc.org/core-2.2.0/Numeric.html#method-i-nonzero-3F http://ruby-doc.org/core-2.2.0/Numeric.html#method-i-nonzero...) returns self or nil.
This misunderstanding of what "?" methods are allowed to return is an ongoing problem in the community, e.g. https://github.com/rails/rails/issues/20110 https://github.com/rails/rails/issues/20110