How to check if a Ruby object is a Boolean

前端 未结 9 1041
迷失自我
迷失自我 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:39

    If your code can sensibly be written as a case statement, this is pretty decent:

    case mybool
    when TrueClass, FalseClass
      puts "It's a bool!"
    else
      puts "It's something else!"
    end
    

提交回复
热议问题