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:

  1. what operator do?
  2. when should 1 use it?
  3. 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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -