0

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

sawa
  • 165,429
  • 45
  • 277
  • 381
Jechol Lee
  • 145
  • 10

2 Answers2

2

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

sawa
  • 165,429
  • 45
  • 277
  • 381
1

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

Reactormonk
  • 21,472
  • 14
  • 74
  • 123
  • 1
    There was a legitimate use during the transition period from 1.8 to 1.9, because it always returned the same thing that indexing string would return. So, on 1.8 `'Hello'[0] == 72` and `?H == 72` and on 1.9 `'Hello'[0] == 'H'` and `?H == 'H'`, so that it in both cases it is *always* true that `'Hello'[0] == ?H` no matter whether you are on 1.8 or 1.9. – Jörg W Mittag Mar 08 '15 at 13:25