How to Check If a Ruby Object Is a Boolean

How to check if a Ruby object is a Boolean

Simplest way I can think of:

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

rails 3.0.3 check if boolean value is true

if this line works:

if item.active == true

then

if item.active

will also work. if item.active? works only if there is a method whose name is actually active?, which is usually the convention for naming a method that returns true or false.

Correct way to check against boolean values in Rails

Different databases store booleans in different ways. Mysql stores them as 0 or 1. These are translated into false and true by rails. Generally you you treat them as booleans in your ruby code.

This code

if is_admin == true

is redundant. you might as well just say

if is_admin

since the result will be the same either way.

Rspec rails : check if object.value is false

You don't need to convert the string to boolean, you can compare strings as well, but in that case you need to use eq instead of be, as they are different objects with the same value:

expect(automatically_send_report.value).to eq "false"

Determining Truthiness of an expression?

You could try (true & value) == value. The part in parentheses seems to always return a boolean; if the value wasn't originally a bool, then it won't be equal to the result. A bool, however, will.

Nil and boolean in Ruby

The statement goes through the conditions in order, will stop when a falsy result is obtained and return the value of the last evaluation performed.

In contrary to && which stops at a falsy value, || will stop at a truthy value instead.

Determining type of an object in ruby

The proper way to determine the "type" of an object, which is a wobbly term in the Ruby world, is to call object.class.

Since classes can inherit from other classes, if you want to determine if an object is "of a particular type" you might call object.is_a?(ClassName) to see if object is of type ClassName or derived from it.

Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called "Duck typing". In other words, if it responds to the methods you want, there's no reason to be particular about the type.

For example, object.is_a?(String) is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. object.respond_to?(:to_s) would be a better way to test that the object in question does what you want.

check several booleans at once in ruby

You don't need to test for == true; it is enough to simply test the "truthiness" of something. Anything that is not nil or false will evaluate to true in a boolean context. So it is sufficient to do:

step_finished = object.one && object.two && object.three && object.four

You can also use all?:

step_finished = [object.one, object.two, object.three, object.four].all?

Ruby/Rails ActiveRecord attribute find boolean

There is another way to detect if a value is a boolean in ruby.

!!value == value #true if boolean

But I don't think there is any method built-in for that.



Related Topics



Leave a reply



Submit