32

I have a DB field that is integer type and values are always 0 or 1. How do I grab the equivalent boolean value in ruby? such that when i do the following the check box is set appropriately:

<%= check_box_tag 'resend', @system_config.resend %>

4 Answers 4

62

You could use the zero? method. It returns true if 0. If you need to backwards, you could easily negate it to !@system_config.resend.zero?. Or you could extend the Fixnum class, adding a to_b? method, since everything is open and extensible in dynamic languages.

class Integer
  def to_b?
    !self.zero?
  end
end

alt text

Ruby API: http://ruby-doc.org/core/classes/Fixnum.html#M001050

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Jarret. Part of my solution was also to use the check_box_tag helper properly like so: <%= check_box_tag 'resend', @system_config.resend, @system_config.resend %>
Note that there's also nonzero? which may read better than !zero?.
I would very strongly recommend not adding functions to built-in classes. I come from a more javascript world where there are multiple times where that comes back to bite people, but more than just that: Everyone reading the code will know what nonzero? function is, but would have no idea what to_b? is!
Just be careful with nonzero? - it returns truthy and falsy values, but not true as zero? does (they are defined in different classes, see ruby-doc.org/core-1.9.3/Fixnum.html#method-i-zero-3F versus ruby-doc.org/core-1.9.3/Numeric.html#method-i-nonzero-3F
@chesterbr I agree. This just threw me for a loop right now. I looked up why nonzero? did not return a boolean as most ruby predicate methods do and came upon this ticket debating getting this corrected bugs.ruby-lang.org/issues/9123. Seems not everyone agrees this is an issue so it unfortunately doesn't seem to be going anywhere.
|
14

1 is your only truth value here. So you can get the boolean truth value with number == 1.

3 Comments

Maybe this could be worded more clearly. I almost thought you meant this to be ruby standard. The accepted answer seem to give a ruby-standard-compatible solution, that happens to also fit with the actual question.
@Simon B.: I'm not sure what about my answer seems "non-standard" to you. Both the == and zero? methods are standard Ruby. But it so happens that number == 1 reads more clearly than !number.zero? IMO.
In ruby, both 0 and 1 are "truthy", so actually there's no chance to make any answer here both "standard" and answer the question. Sorry. Maybe I better to try to get this into the question text or title. Anyone coming here looking for "ruby-faithful type casting" from Fixnum to Boolean will otherwise get confused either way. Also, according to drawingablank.me/blog/ruby-boolean-typecasting.html is seems !! is the only ruby-faithful option. Which is not an option in the question above.
4

With Rails there's also ActiveModel::Type::Boolean.new.cast(value) which makes 0 false, non-zero true, [] true, {} true. Note that nil -> nil.

Comments

2

I had the same problem and dealt with it this way:

def to_boolean(var)
  case var
    when true, 'true', 1, '1'
      return true
    when false, 'false', 0, '0'
      return false
  end
end

This works with forms, databases and people who don't know Ruby. I found it to be useful especially with Rails, since parameters are often passed/interpreted as strings and I don't want to worry about that.

2 Comments

The indentation here is non-standard. Simpler form would be def to_boolean(value) ['true',1,'1',true].include?(value) end
return keyword is redundant here. Also you could use else case to return false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.