31

false and nil evaluate to false in Ruby. Anything else? Please provide links to official/authoritative references.

2.0.0p247 :001 > if true ; puts 'TRUE' ; else puts 'FALSE' ; end
TRUE

2.0.0p247 :002 > if false ; puts 'TRUE' ; else puts 'FALSE' ; end
FALSE

2.0.0p247 :003 > if nil ; puts 'TRUE' ; else puts 'FALSE' ; end
FALSE

2.0.0p247 :004 > if 0 ; puts 'TRUE' ; else puts 'FALSE' ; end
TRUE

2.0.0p247 :005 > if [] ; puts 'TRUE' ; else puts 'FALSE' ; end
TRUE

2.0.0p247 :006 > if {} ; puts 'TRUE' ; else puts 'FALSE' ; end
TRUE

2.0.0p247 :007 > if '' ; puts 'TRUE' ; else puts 'FALSE' ; end
(irb):616: warning: string literal in condition
TRUE
user664833
  • 18,397
  • 19
  • 91
  • 140

5 Answers5

43

false and nil are the only ones:

http://www.ruby-doc.org/core-2.1.1/FalseClass.html

Rails provides present? which also includes empty strings and empty arrays: http://api.rubyonrails.org/classes/Object.html#method-i-present-3F

nzifnab
  • 15,876
  • 3
  • 50
  • 65
  • 2
    "false and nil are the only ones" Unless you have a custom class that defines `!@`, which can return `true`, `false`, `"foobar"`, whatever. – Shelvacu Oct 09 '19 at 17:08
9

You just found them all

In Ruby, false and nil are “falsy”, while all other values are “truthy”

as Yehuda Katz mentioned in his blog post in 2009

tessi
  • 13,313
  • 3
  • 38
  • 50
2

The only false-y values in Ruby are false and nil, as attested to by the book "The Ruby Programming Language", by David Flanagan and Yukihiro Matsumoto (the Matz):

Predicates typically return one of the Boolean values true or false, but this is not required, as any value other than false or nil works like true when a Boolean value is required. (The Numeric method nonzero?, for example, returns nil if the number it is invoked on is zero, and just returns the number otherwise.)

(emphasis mine)

This quote can be found on page 180 of the book.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

I can only agree, that nil and false are indeed the only two values, that evaluate to false in Ruby. I just wanted to point to another useful resource:

True and False vs. "Truthy" and "Falsey" (or "Falsy") in Ruby, Python, and JavaScript

Graham
  • 7,431
  • 18
  • 59
  • 84
Steve
  • 1,553
  • 2
  • 20
  • 29
0

Note that even though false and nil are the only objects that result in false in a Boolean context, that's when tested directly in a Boolean context, e.g., the object in question corresponds to the expression being tested in an if.

In particular, an object can still customize the logical not operator. That is, how the unary ! operator and the keyword not behave, and this can lead to surprises.

class FakeFalsy
    def !
        true
    end
end

FakeFalsy above defines a ! method customizing this way the behaving of the logical not. Note that an instance of FakeFalsy isn't either false or nil:

>> obj = FakeFalsy.new
...
>> obj.equal? false
=> false
>> obj.equal? nil
=> false

Yet it may still look that the object being tested is either false or nil:

>> puts "It seems to be false or nil" if !obj
It seems to be false or nil
=> nil
puts "It seems to be false or nil" if not obj
It seems to be false or nil
=> nil
JFMR
  • 23,265
  • 4
  • 52
  • 76