272

What is the purpose of the question mark operator in Ruby?

Sometimes it appears like this:

assert !product.valid?

sometimes it's in an if construct.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
rtacconi
  • 14,317
  • 20
  • 66
  • 84
  • 3
    Question mark at the end of a function is not an operator, it's an ordinary character. Question mark along with colon is a ternary conditional operator. Question mark by itself is a unary quotation operator. See http://stackoverflow.com/questions/16641205/what-does-the-unary-question-mark-operator-do – Old Pro Jan 07 '17 at 02:01

9 Answers9

372

It is a code style convention; it indicates that a method returns a boolean value (true or false) or an object to indicate a true value (or “truthy” value).

The question mark is a valid character at the end of a method name.

https://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Method+Names

Saeed
  • 43
  • 1
  • 8
chillitom
  • 24,888
  • 17
  • 83
  • 118
  • 14
    Thanks, but does this convention mean it does the _logic only_ or could there be side effects? Funny how this is sort of a hack around a typed languages with the return type in the code. – Jason Nov 20 '12 at 21:36
  • 9
    It this case it is strictly a method naming convention. There is no enforcement of a boolean return value. Just because the method name ends in a question mark doesn't mean that the method *must* return a boolean ...but it most definitely *should* return a boolean. However there are several other uses of the `?` in Ruby (is a ternary operator, converting a character to its ASCII integer, usage in `test`, in RegEx'es, etc.) many of which are covered in the other answers here. – Karl Wilbur Sep 11 '15 at 20:36
  • 6
    @Jason FYI, even strongly typed languages have conventions like this. For example, in Java methods that return a boolean value are often prefixed with "is", as in `isEmpty()`, `isDigit()`, etc. In Ruby, the same methods would be written `empty?`, and `digit?` which is quite a bit nicer IMO. – Ajedi32 Dec 28 '15 at 17:27
  • 3
    Ajedi32 - Yeah I agree it's nice to name functions like that, I use `is` all the time. This might be super confusing for someone coming from Swift since the question mark there is used for `nullable` : ) – Jason Dec 29 '15 at 19:51
  • I believe the actual term for Ruby methods ending in a "?" is called: predicate methods – J.R. Bob Dobbs Mar 11 '22 at 14:48
88

Also note ? along with a character acts as shorthand for a single-character string literal since Ruby 1.9.

For example:

?F # => is the same as "F"

This is referenced near the bottom of the string literals section of the ruby docs:

There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding:

?a       #=> "a"
?abc     #=> SyntaxError
?\n      #=> "\n"
?\s      #=> " "
?\\      #=> "\\"
?\u{41}  #=> "A"
?\C-a    #=> "\x01"
?\M-a    #=> "\xE1"
?\M-\C-a #=> "\x81"
?\C-\M-a #=> "\x81", same as above
?あ      #=> "あ"

Prior to Ruby 1.9, this returned the ASCII character code of the character. To get the old behavior in modern Ruby, you can use the #ord method:

?F.ord # => will return 70
Taywee
  • 1,313
  • 11
  • 17
Greg Osuri
  • 889
  • 6
  • 2
55

It's a convention in Ruby that methods that return boolean values end in a question mark. There's no more significance to it than that.

Eifion
  • 5,403
  • 2
  • 27
  • 27
30

In your example it's just part of the method name. In Ruby you can also use exclamation points in method names!

Another example of question marks in Ruby would be the ternary operator.

customerName == "Fred" ? "Hello Fred" : "Who are you?"
Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
  • 14
    To expand on Andy's answer you will also see things like: customerName == user.logged_in? ? user.name : "Who are you?" Note the double question mark – Question Mark Aug 28 '09 at 09:13
24

It may be worth pointing out that ?s are only allowed in method names, not variables. In the process of learning Ruby, I assumed that ? designated a boolean return type so I tried adding them to flag variables, leading to errors. This led to me erroneously believing for a while that there was some special syntax involving ?s.

Relevant: Why can't a variable name end with `?` while a method name can?

Rich Smith
  • 1,675
  • 12
  • 24
20

In your example

product.valid?

Is actually a function call and calls a function named valid?. Certain types of "test for condition"/boolean functions have a question mark as part of the function name by convention.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
13

I believe it's just a convention for things that are boolean. A bit like saying "IsValid".

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
2

It's also used in regular expressions, meaning "at most one repetition of the preceding character"

for example the regular expression /hey?/ matches with the strings "he" and "hey".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
José Joel.
  • 2,040
  • 6
  • 28
  • 46
0

It's also a common convention to use with the first argument of the test method from Kernel#test

test ?d, "/dev" # directory exists?
# => true
test ?-, "/etc/hosts", "/etc/hosts" # are the files identical
# => true

as seen in this question here

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
jtzero
  • 2,204
  • 2
  • 25
  • 44
  • 4
    It’s not related to `test`, `?d` is a shortcut for `"d"`. `test` takes a one-char string as its first argument, so you can call it with `test ?d, "/dev"` or `test "d", "/dev"`. – bfontaine Apr 24 '15 at 17:54