What does the unary question mark (?) operator do?

前端 未结 4 565
傲寒
傲寒 2020-11-29 10:55

I saw this operator in HAML code. I wonder what it is for.

I see the following works:

> ?{
=> \"{\" 
> ?\\s
=> \" \" 
> ?a
=> \"a\"         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 11:37

    It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

提交回复
热议问题