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

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

    “?” mark in a ruby method indicates that method will return either true or false.

    After checking method name we can determine that this method is going to return boolean value.

    Example:

    empty? - This method will check whatever the object is empty or not. Depending on that it will return true or false.

    Uses:

    [1,2,3].empty? - return false

    [].empty? - return true

提交回复
热议问题