ruby - What does the unary question mark (?) operator do? -
i saw operator in haml code. wonder for.
i see following works:
> ?{ => "{" > ?\s => " " > ?a => "a"
and doesn't work:
> ?ab syntaxerror: (irb):4: syntax error, unexpected '?'
so suppose takes character argument , returns string character.
questions:
- what operator do?
- when should 1 use it?
- if creates one-character string, why included in language? doesn't break language orthogonality? benefit?
it returns single character string. shortest way write single-character string literal. use when want define lot of single-character strings. heritage ruby <1.9, used return ascii code character. don't understand mean "break language orthogonality".
Comments
Post a Comment