What does the question mark at the end of a method name mean in Ruby?

Ruby

Ruby Problem Overview


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.

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

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

Solution 3 - Ruby

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.

Solution 4 - Ruby

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?"

Solution 5 - Ruby

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: https://stackoverflow.com/questions/5448938/in-ruby-a-variable-name-cannot-end-with-such-as-has-completed-but-a-meth

Solution 6 - Ruby

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.

Solution 7 - Ruby

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

Solution 8 - Ruby

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".

Solution 9 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrtacconiView Question on Stackoverflow
Solution 1 - RubychillitomView Answer on Stackoverflow
Solution 2 - RubyGreg OsuriView Answer on Stackoverflow
Solution 3 - RubyEifionView Answer on Stackoverflow
Solution 4 - RubyAndy GaskellView Answer on Stackoverflow
Solution 5 - RubyRich SmithView Answer on Stackoverflow
Solution 6 - RubyTimo GeuschView Answer on Stackoverflow
Solution 7 - RubyNeil BarnwellView Answer on Stackoverflow
Solution 8 - RubyJosé Joel.View Answer on Stackoverflow
Solution 9 - RubyjtzeroView Answer on Stackoverflow