How to check if a Ruby object is a Boolean

前端 未结 9 1048
迷失自我
迷失自我 2020-11-30 18:57

I can\'t seem to check if an object is a boolean easily. Is there something like this in Ruby?

true.is_a?(Boolean)
false.is_a?(Boolean)

Ri

9条回答
  •  感动是毒
    2020-11-30 19:45

    So try this out (x == true) ^ (x == false) note you need the parenthesis but this is more beautiful and compact.

    It even passes the suggested like "cuak" but not a "cuak"... class X; def !; self end end ; x = X.new; (x == true) ^ (x == false)

    Note: See that this is so basic that you can use it in other languages too, that doesn't provide a "thing is boolean".

    Note 2: Also you can use this to say thing is one of??: "red", "green", "blue" if you add more XORS... or say this thing is one of??: 4, 5, 8, 35.

提交回复
热议问题