concept false in category ruby

appears as: false
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

false is a special term in Ruby, as is the term true. true and false are objects. Ruby uses them to represent results of, among other things, comparison operations (like x > y), and you can use them to represent truth and falsehood in your own methods. You may have noticed that the method name available? ends with a question mark. Ruby lets you do this so you can write methods that evaluate to true or false and make the method calls look like questions:

if ticket.available?
  puts "You're in luck!"
else
  puts "Sorry--that seat has been sold."
end

But there’s more to truth and falsehood than the true and false objects. Every expression in Ruby evaluates to an object, and every object in Ruby has a truth value. The truth value of almost every object in Ruby is true. The only objects whose truth value (or Boolean value) is false are the object false and the special nonentity object nil. You’ll see Boolean values and nil in more detail in chapter 7. For the moment, you can think of both false and nil as functionally equivalent indicators of a negative test outcome.

  • Boolean states and objects, and nil—A close look at true and false and related concepts in Ruby
  • The Boolean objects true and false are special objects, each being the only instance of a class especially created for it: TrueClass and FalseClass, respectively. You can ask true and false to tell you their classes’ names, and they will:

    puts true.class          #1
    puts false.class         #2

    The terms true and false are keywords. You can’t use them as variable or method names; they’re reserved for Ruby’s exclusive use.

    You can pass the objects true and false around, assign them to variables, and examine them like any other object. Here’s an irb session that puts true through its paces in its capacity as a Ruby object:

    >> a = true
    => true
    >> a = 1 unless a
    => nil
    >> a
    => true
    >> b = a
    => true

    You’ll sometimes see true and false used as method arguments. For example, if you want a class to show you all of its instance methods but to exclude those defined in ancestral classes, you can provide the argument false to your request:

    Sometimes you’ll want to see the instance methods defined in a particular class without bothering with the methods every object has. After all, you already know that your object has those methods. You can view a class’s instance methods without those of the class’s ancestors by using the slightly arcane technique, introduced earlier, of providing the argument false to the instance_methods method:

    Rails 4 in Action: Revised Edition of Rails 3 in Action

    This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

    Just as in the show and edit actions, the @purchase object is first fetched by the call to before_action :set_purchase. The parameters from the form are sent through in the same fashion as they were in the create action, coming through as purchase_params. Rather than instantiating a new object by using the new class method, you use update on the existing @purchase object. This does what it says: updates the attributes. What it doesn’t say, though, is that it validates the attributes and, if the attributes are valid, saves the record and returns true. If they aren’t valid, it returns false.

    But you’ll want to modify this migration so that when users are created, the admin field is set to false rather than defaulting to nil. Even though nil is “falsey” in Ruby, it’s clearer to make it explicitly false. nil means users have no admin information, but they do: they’re not an admin. It’s better to be explicit about things.