Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:50:05+00:00 2026-05-13T23:50:05+00:00

Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are

  • 0

Possible Duplicate:
Ruby: difference between || and ‘or’

Using Ruby

||

and

or 

are very common practices which makes it important to know the difference between the two as unfortunately I am not sure.

First of all my question is if the following assumption is correct:

EX1:

if @variable_1 || @variable_2 || @variable_3 
  do something 
else
  do nothing
end

EX2:

if @variable_1 or @variable_2 or @variable_3
  do something 
else
  do nothing
end

So in the first example if any variable is false then it will execute “do nothing”

However, for the second example all variables are checked and if one is true then it will execute “do something”.

In summary use “||” if you have a list of variables that need to be checked and if one of them returns false then a flag goes up. Use the second example with a list of variables where only one needs to be true in order to continue executing the desired code.

Are these assumptions correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:50:05+00:00Added an answer on May 13, 2026 at 11:50 pm

    or the second example all variables are checked and if one is true then it will execute “do something”.

    This is false sentence.

    As a result your assumptions are not correct.

    Both or and || do the same thing.

    The main difference is that or has lower precedence than ||. So you should pay attention to more complex evaluations:

    # Simple cases are not confusing
    false || true # true
    false or true # true
    
    # This is more complex
    a = false || true # a=true
    a = false or true # a=false
    
    # Also similarly as 1 + 2*3 returns 7, the following returns true:
    false or false||true # true
    # BUT! THIS IS IMPORTANT!
    a = false or false||true   # a=false
    a = (false or false||true) # a=true
    

    Here is a list of operators precedence.

    So the real difference will be noticed if you use the expression that includes any of the following operators:

    • .. ... – Range (inclusive and exclusive)
    • ? : – Ternary if-then-else
    • = %= { /= -= += |= &= >>= <<= *= &&= ||= **= – Assignment
    • defined? – Check if specified symbol defined
    • not – Logical negation
    • and – Logical composition

    there might be others too.

    You can thing about the difference between those as different between + and *: ||==* and or=+. The same applies to and and not.

    You should really pay attention to that.

    Personally I prefer || operator as its semantics is well understood and avoid or.

    While it ‘feels’ like or is more friendly in many cases (see my code sample), even in trivial ones, it is a source of bugs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between “and” and && in Ruby? Ruby: difference between || and
Possible Duplicate: Ruby: difference between || and 'or' In ruby, isn't 'or' and '||'
Possible Duplicate: Ruby - What is the difference between map, each and collect? I
Possible Duplicate: Difference between -%> and %> in rails I need to know what
Possible Duplicate: What is the difference between include and require in Ruby? require File.join(File.dirname(__FILE__),
Possible Duplicate: Ruby Refuses to Divide Correctly I am using Ruby on Rails 3.2.2
Possible Duplicate: === vs. == in Ruby I was wondering what the difference is
Possible Duplicate: What's the difference between POST and raw POST in PHP at all?
Possible Duplicate: Ruby on Rails keybard shortcuts Hi all - does anyone know how
Possible Duplicate: Is there any difference between :key => value and key: value assignments?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.