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

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

    Re #2, a place I've found it useful is in conveying that a parameter I'm setting or value I'm testing for is intended to be a single character and not just that this happened to simply be a short string. It's a subtle readability/documentation thing, but worth considering for later maintainers (including myself).

提交回复
热议问题