Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Or Keyword in Ruby

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Ruby, “or” keyword returns the logical disjunction of its two operands. The condition becomes true if both the operands are true. It returns “true” when any one condition/expression is “true” and returns “false” only when all of them are “false”. This keyword is an equivalent of || logical operator in Ruby, but with lower precedence.

Syntax:  

Condition1 or Condition2

Example 1:

Ruby




# Ruby program to illustrate or keyword
username = "geek"
password = "come"
  
# Using or keyword
if (username == "geek" or password == "come")
puts "Welcome, GeeksforGeeks!"
else  
puts "Incorrect username or password!"
end


Output: 

Welcome, GeeksforGeeks!

Example 2: 

In this example, we will see the precedence difference in or keyword and || operator:

Ruby




# Ruby program to illustrate or keyword
# and || operator
  
def one() true; end
def two() true; end
      
# Using || operator    
res1 = one || two ? "GeeksforGeeks" : "Do Nothing"
puts res1
  
# Using or keyword
res2 = one or two ? "GeeksforGeeks" : "Do Nothing"
puts res2


Output:

GeeksforGeeks
true

Explanation: In the above example, on the primary look, logic is that the same, but we get different results. But when you take a look closely, you will see the difference. In the first case output is GeeksforGeeks and in second case output is true. This is related, of course, with operator precedence. Think about the order in which they are evaluated (the precedence).

  1. ||
  2. =
  3. Or

Because || has higher precedence than = in the first statement(i.e, using || operator):

res1 = one || two ? “GeeksforGeeks” : “Do Nothing”

In the second statement(i.e, using or keyword), the order of those operations is different = has higher precedence:

res2 = one or two ? “GeeksforGeeks” : “Do Nothing”


My Personal Notes arrow_drop_up
Last Updated : 27 Jul, 2020
Like Article
Save Article
Similar Reads