Open In App

not Keyword in Ruby

Improve
Improve
Like Article
Like
Save
Share
Report

The keyword “not” is different from the others. The “not” keyword gets an expression and inverts its boolean value – so given a true condition it will return false. It works like “!” operator in Ruby, the only difference between “and” keyword and “!” operator is  “!” has the highest precedence of all operators, and “not” one of the lowest.

Syntax:

not expression

Example 1:

Ruby




# Ruby program to illustrate not keyword
uname = "geeks"
  
# Using not keyword
if not(uname == "Geeks" )
puts "Incorrect username!"
else  
puts "Welcome, GeeksforGeeks!"
end


 

Output:

Incorrect username!

Example 2: 

 

Ruby




# Ruby program to illustrate not keyword
uname = "Geek"
password = "Wel123"
number = 123
if not(uname == "Geek" && 
       password == "Wel123" &&
       number == 123)
puts "Hey, Incorrect Credentials"
else
  puts "Welcome to GeeksforGeeks"  
end


 

Output:

Welcome to GeeksforGeeks


Last Updated : 27 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads