Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the unary question mark (?) operator do?

Tags:

operators

ruby

I saw this operator in HAML code. I wonder what it is for.

I see the following works:

> ?{
=> "{" 
> ?\s
=> " " 
> ?a
=> "a" 

And this doesn't work:

> ?ab
SyntaxError: (irb):4: syntax error, unexpected '?'

So I suppose that it takes a character a argument and returns a string with that character.

questions:

  1. What does this operator do?
  2. When should one use it?
  3. If it really only creates a one-character string, why was it included in the language? Doesn't it break the language orthogonality? What is the benefit?
like image 252
fotanus Avatar asked May 20 '13 01:05

fotanus


People also ask

What does question mark operator do?

“Question mark” or “conditional” operator in JavaScript is a ternary operator that has three operands. The expression consists of three operands: the condition, value if true and value if false. The evaluation of the condition should result in either true/false or a boolean value.

What does question mark operator do in C++?

The question mark operator, ?:, is also found in C++. Some people call it the ternary operator because it is the only operator in C++ (and Java) that takes three operands. If you are not familiar with it, it's works like an if-else, but combines operators.

What does question mark in Rust mean?

operator in Rust is used as an error propagation alternative to functions that return Result or Option types. The ? operator is a shortcut as it reduces the amount of code needed to immediately return Err or None from the types Result<T, Err> or Option in a function.

What does question mark do in 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.


3 Answers

It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

like image 167
sawa Avatar answered Oct 22 '22 10:10

sawa


It's not an operator, it's a character literal. However, there is no character type in Ruby, so instead of an instance of a character type the character literal evaluates to the "default representation of a character". In Ruby 1.9+, that's a String of length 1, in Ruby 1.8, it's a Fixnum denoting the Unicode codepoint of the character.

like image 34
Jörg W Mittag Avatar answered Oct 22 '22 12:10

Jörg W Mittag


Re #2, a place I've found it useful is in conveying that a parameter I'm setting or value I'm testing for is intended to be a single character and not just that this happened to simply be a short string. It's a subtle readability/documentation thing, but worth considering for later maintainers (including myself).

like image 2
user2391694 Avatar answered Oct 22 '22 10:10

user2391694