2

Why when I use a question mark at the beginning of a line it makes it into a String?

For example:

?a
# => "a"

?1
# => "1"

?(
# => "("

?a + "b"
# => "ab"

When I use more than 1 character it raises error:

?ab
# SyntaxError (syntax error, unexpected '?')

Why it happens?

mechnicov
  • 12,025
  • 4
  • 33
  • 56

1 Answers1

1

I found the answer in 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
?あ      #=> "あ"
mechnicov
  • 12,025
  • 4
  • 33
  • 56