How to check if a Ruby object is a Boolean

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

    I find this to be concise and self-documenting:

    [true, false].include? foo
    

    If using Rails or ActiveSupport, you can even do a direct query using in?

    foo.in? [true, false]
    

    Checking against all possible values isn't something I'd recommend for floats, but feasible when there are only two possible values!

提交回复
热议问题