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

前端 未结 4 558
傲寒
傲寒 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:26

    It's not an operator, it's a character literal. However, there is no character type in Ruby, so instead of an instance of a character type the character literal evaluates to the "default representation of a character". In Ruby 1.9+, that's a String of length 1, in Ruby 1.8, it's a Fixnum denoting the Unicode codepoint of the character.

提交回复
热议问题