I need to check on a Boolean variable whether its value has been set or not, I would know that by checking if it contains an empty string ""
or nil
indicates the value has not been set and another value as true
or false
indicates the value has been set.
I tried using blank?
but Rails has this gotcha on the blank?
method that when called on false
will actually return true
false.blank? # Returns true
So, when the value of the variable had been set to false, it would give me false negatives for the value as if the variable wouldn't have been set.
How to check that a variable is not set(""
, nil
) or it is set(true
,false
, 0
, 1
) in Ruby on Rails?
is_set()
suggestion in my answer is probably the best solution. It will cover all cases OP needs, including exceptional things. (like arrays and hashes or weirder stuff than that.) – Finecut