- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
True, False and Nil in Ruby Programming
We know that everything in Ruby is treated as an object, and so the true, false and nil as well. They are built-in types that Ruby provides to do different conditional checks and more. In this article, we will explore different examples of the true, false and nil data types and how to use them.
True, False in Ruby
Let's start with a very simple example where we will check if two variables are equal or not.
Example 1
Consider the code shown below
first = 10 second = 10 if first == second # If Condition is true puts "True! First and second are equal" else # If Condition is false puts "False! First and second are not equal" end
Output
It will generate the following output −
True! First and second are equal
Example 2
Let's consider another example of True, False. Here, we will compare two strings.
a1 = "The quick brown fox jumps over the lazy dog." b1 = "The quick brown fox jumps over the lazy dog." result1 = a1 == b1 puts result1 a2 = "An apple a day keeps the doctor away." b2 = "An orange a day keeps the doctor away." result2 = a2 == b2 puts result2
Output
It will generate the following output −
true false
Nil in Ruby
Now that we have some idea about how to make use of the True, False built-in types in Ruby, let's explore some code examples of Nil as well.
Example 3
Consider the code shown below
arr = [ 1, 2, 3, 4, 5 ] # Since arr[5] does not exist, it is nil. res1 = arr[5].nil? puts res1 # Since arr[2] exists, it is not nil. res2 = arr[2].nil? puts res2
Output
It will generate the following output −
true false
- Related Articles
- Static Members in Ruby Programming
- Yield keyword in Ruby Programming
- Transpose() function in Ruby Programming
- How does Recursion work in Ruby Programming?
- Are true and false keywords in java?
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- State True or False-
- What are False Positives and True Positives in Cybersecurity?
- If ([] == false) is true, why does ([] || true) result in []? - JavaScript
- Jute is produced in Jammu and Kashmir. (true or false)
- Cotton and jute are natural fibres. (True or False)
- Electromagnets are used in motors. True or False?
- How can I count true and false values in my PHP array?
- State true or false: Motion and rest are relative terms.""
- State whether the following statements are true or false. In case a statement is false, write the corrected statement in your notebook.(a) Cutting a log of wood into pieces is a chemical change. (True/ False)(b) Formation of manure from leaves is a physical change. (True/ False)(c) Iron pipes coated with zinc do not get rusted easily. (True/ False)(d) Iron and rust are the same substances. (True/ False)(e) Condensation of steam is not a chemical change. (True/ False)
