Jechol Lee
Jechol Lee

Reputation: 145

Why is `?something` invalid?

?. is a string literal:

?. #=> "."

However, I failed to declare a variable with a name like that:

?some_var = 100 #=> Error

How is?something invalid when ?. is valid?

Upvotes: 0

Views: 61

Answers (2)

Reactormonk
Reactormonk

Reputation: 21700

?a is the same as "a". So it is a value, which belongs on the right hand side of an assignment, not the left hand side. It is not a variable name.

The Syntax exists as a relic from Ruby <=1.9, where it was equivalent to "a".bytes[0] and ?d could be used to shave off one character of code golf. I haven't seen any legitimate use otherwise.

Upvotes: 1

sawa
sawa

Reputation: 168101

? cannot describe any string literal; it is valid only for a single character.

Even if ?something were a valid string literal (counter to fact),

?something = ...

will be assignment to a string, which does not make sense. You cannot assign a value to a string.

Upvotes: 2

Related Questions