Predicate Methods

ruby predicates

A commonly used definition of a predicate method is a method that returns a boolean value. In Ruby, the convention is to end the method name with a question mark.

A predicate method should return true or false. (In the reference article, the author mentions that some people instead advocate for a ‘truthy’ value.)

0.zero? # => true

1.zero? # => false

defined? in Ruby might appear to be an exception to the convention as it does not return true or false, but defined? an operator, not a method.

In Rails, there is a “getter” method generated for each database column by default, like Item#returns_accepted. As a convenience, a question mark method (Item#returns_accepted?) is also generated for boolean database columns which behaves mostly the same but it also converts nil to false.

references

The Elements of Ruby Style: Predicate Methods