Question Mark
The question mark (?
) symbol shows up in a couple places in Ruby. Besides its use in Ternary If expressions (e.g. a = bool ? c : d
), we also see ?
in the following contexts:
Character Literal Notation
> ?a
=> "a"
> ??
=> "?"
Boolean Method Naming Convention
Ruby has a strong method-naming convention for methods that answer a question true
or false
. That convention is that they end with a ?
.
We can look right to the Integer
class for a couple good examples of this:
> 3.odd?
=> true
> 3.even?
=> false
Keep in mind that this is purely a convention. We can communicate intent by following this convention when we define methods in our own code, but it is not syntactically required and it does not have an impact on the behavior of the code.