not Keyword in Ruby

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 



Output:

Incorrect username!

Example 2: 

 

Ruby

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


 

Output:

Welcome to GeeksforGeeks

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.