Ruby is an open-sourced object-oriented programming language developed by Yukihiro Matsumoto. In Ruby, everything is treated as an object. true, false and nil are built-in data types of Ruby.
Note: Always remember in Ruby true, false, and nil are objects, not numbers. Whenever Ruby requires a Boolean value, then nil behaves like false and values other than nil or false behave like true.
True and False
In Ruby, true and false are boolean values that represent yes and no. true is an object of TrueClass and false is an object of FalseClass.
Note: Ruby does not contain Boolean class.
Let’s see a few examples of true and flase in Ruby.
Example 1:
# Ruby program to illustrate the use # of true and false a = 4b = 4 if a == b # If Condition is true puts "True! a and b are equal" else # If Condition is false puts "False! a and b are not equal" end |
Output:
True! a and b are equal
Example 2:
# Ruby program to illustrate the use # of true and false a1 = "GeeksforGeeks"b1 = "GeeksforGeeks"result1 = a1 == b1 puts result1 a2 = "GeeksforGeeks"b2 = "geeks"result2 = a2 == b2 puts result2 |
Output:
true false
Example 3:
# Ruby program to illustrate the use # of true and false # If condition is true if 55 == 55 puts "GeeksforGeeks !" # If Condition is false else puts " A Computer Science Portal for Geeks!" end |
Output:
GeeksforGeeks !
Nil
In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby’s way of referring to nothing or void. Ruby also provide a nil? method to detect if any object is nil or not.
Example 1:
# Checking for nil's class a = nil.classputs a |
Output:
NilClass
Example 2:
# Ruby program to illustrate nil? method # Checking for Nil array = [ 1, 2, 3, 4, 5 ] # Since array[5] does not exist so, it is nil. result1 = array[5].nil? puts result1 # Since array[2] exists so, it is not nil. result2 = array[2].nil? puts result2 |
Output:
true false
Recommended Posts:
- How to check pointer or interface is nil or not in Golang?
- When and why to 'return false' in JavaScript?
- false command in Linux with examples
- When to use PreventDefault( ) vs Return false in JavaScript?
- Why “0” is equal to false in JavaScript ?
- How to append class if condition is true in Haml?
- Similarities and Differences between Ruby and C language
- Similarities and Differences between Ruby and C++
- Ruby | Exception Class and its Methods
- Ruby | Dir Class and its methods
- Ruby | unless Statement and unless Modifier
- Ruby Break and Next Statement
- Ruby Date and Time
- Ruby redo and retry Statement
- Ruby Search and Replace
- BEGIN and END Blocks In Ruby
- Catch and Throw Exception In Ruby
- Ruby getters and setters Method
- Ruby | Class Method and Variables
- Difference Between Laravel and Ruby on Rails
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

