How to check if a Ruby object is a Boolean

Lance Pollard picture Lance Pollard · Jun 12, 2010 · Viewed 104.5k times · Source

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)

Right now I'm doing this and would like to shorten it:

some_var = rand(1) == 1 ? true : false
(some_var.is_a?(TrueClass) || some_var.is_a?(FalseClass))

Answer

Konstantin Haase picture Konstantin Haase · Jun 13, 2010

Simplest way I can think of:

# checking whether foo is a boolean
!!foo == foo