Which Values Are Considered Falsy in Ruby?

List of all falsy values in Ruby

In Ruby, only the following two values are considered falsy (i.e. objects that evaluate to false in a boolean context):

  1. false;
  2. nil.

For example, when you use either of these in a conditional statement, they're evaluated to boolean false:

if nil
    puts "not printed"
else
    puts "printed"
end

#=> "printed"
if false
    puts "not printed"
else
    puts "printed"
end

#=> "printed"

Other than these two values, everything else in Ruby is considered a truthy value, including an empty array, empty string, 0, etc.:

if []
    puts "is truthy"
end

#=> "is truthy"
if ""
    puts "is truthy"
end

#=> "is truthy"
if 0
    puts "is truthy"
end

#=> "is truthy"

Hope you found this post useful. It was published . Please show your love and support by sharing this post.