The Ruby Programming Language (2024)

We saw in Keywords that true, false, and nil are keywords in Ruby. true and false are the two Boolean values, and they represent truth and falsehood, yes and no, on and off. nil is a special value reserved to indicate the absence of value.

See Also
Ruby's Rules

Each of these keywords evaluates to a special object. true evaluates to an object that is a singleton instance of TrueClass. Likewise, false and nil are singleton instances of FalseClass and NilClass. Note that there is no Boolean class in Ruby. TrueClass and FalseClass both have Object as their superclass.

If you want to check whether a value is nil, you can simply compare it to nil, or use the method nil?:

o == nil # Is o nil?o.nil? # Another way to test

Note that true, false, and nil refer to objects, not numbers. false and nil are not the same thing as 0, and true is not the same thing as 1. When Ruby requires a Boolean value, nil behaves like false, and any value other than nil or false behaves like true.

The Ruby Programming Language (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6556

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.