- 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
How to use the "not" keyword in Ruby?
In Ruby, we use the not keyword when we want to get an expression and then invert its Boolean value. In simple words, if an expression evaluates to True, then by using the not keyword, we will get False as the result of the expression.
It can be said that the not keyword works like the "!" operator in Ruby, but the only difference between them is that the "!" operator has the highest precedence of all operators and the "not" operator has the lowest.
Syntax
Here is the syntax of the not keyword in Ruby
not expression
Now, let's take a couple of examples and see how to use the not keyword in Ruby.
Example 1
Consider the code shown below.
first_variable = "ruby" # Using not keyword if not(first_variable == "ruby") puts "The variable does not match." else puts "Learn How to Program in Ruby!" end
In the above example, we declared a variable named "first_variable" and then we used the not keyword in combination with the if condition.
Output
If we write the following code on any Ruby IDE, then we will get the following output in the terminal.
Learn How to Program in Ruby!
Example 2
Now let's take a slightly more complex code of the not operator in Ruby. Consider the code shown below.
variable_1 = "tutorialspoint" variable_2 = "ruby_programming" constant = 12345 # if with not keyword if not(variable_1 == "tutorialspoint" && variable_2 == "ruby_programming" && constant == 12345) puts "Variables Don't Match. Please Check." else puts "Hi, How Are You Doing?" end
Output
On execution, it will produce the following output.
Hi, How Are You Doing?
In the above code, we are using the not keyword in the if statement and also clubbing together multiple && statements to form an expression. Since the entire conditions of the && expression are True and then we flip the value using the not operator, that's why we get the above output.
- Related Articles
- How to use the "defined?" keyword in Ruby?
- How to use the "or" keyword in Ruby?
- How to use the 'and' keyword in Ruby?
- Yield keyword in Ruby Programming
- How to use BigDecimal in Ruby?
- How to use global variables in Ruby?
- How to Use Instance Variables in Ruby
- How to use the debugger keyword in JavaScript?
- How to use the readonly keyword in TypeScript?
- How to use void keyword in JavaScript?
- How to use the 'with' keyword in JavaScript?
- How do I use Selenium with Ruby?
- How to use 'const' keyword in JavaScript?
- How to use the 'break' and 'next' statements in Ruby?
- How to set what browsers will show that do not support the ruby element?
