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

  • Home
  • SEARCH
  • 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 8704757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:11:22+00:00 2026-06-13T03:11:22+00:00

So I am writing a Ruby program for school that changes the value of

  • 0

So I am writing a Ruby program for school that changes the value of a boolean to true if a certain value is either 1 or 3, and to false if it is 0 or 2. Since I come from a Java background, I thought that this code should work: if n == 1 || n == 3

But it does not. So my question here is is it possible to use an “Or” expression thingy in If blocks in Ruby? I know that my current situation could be solved easily by just something like the following:

if n == 0
    t_o_f = false
elsif n == 1
    t_o_f = true

Et Cetera. But I want to know if I can use an Or in If blocks for the future.

  • 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-06-13T03:11:23+00:00Added an answer on June 13, 2026 at 3:11 am

    Yes, any expression can be used in an if condition, including those using the || (logical or) operator.

    As with Java, Ruby’s || operator short-circuits. That is, if the left side is true, the right side is not evaluated.

    Idiomatic ruby uses postfix if for one-liners:

    puts "Yes" if n == 4 || n == 5
    

    Avoid postfix if the line is long, however, and break it into multiple lines:

    if n == 4 || n == 5
      puts "This is a big long string to be output....................."
    end
    

    That’s because postfix if can get visually lost at the end of a long line.

    You can have a one-liner with prefix if, but it’s not commonly seen:

    if n == 4 || n == 5 then puts "foo" end
    

    or, perhaps:

    if n == 4 || n == 5 ; puts "foo" ; end
    

    This is really a multi-line if condensed into one line; the semicolons act as line breaks.

    When testing for multiple conditions, it can sometimes be valuable to use Array#include?:

    puts "foo" if [4, 5].include?(n)
    

    The value of this for only two conditions is not that great. For three or more, it reads well.

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

Sidebar

Related Questions

Overview I am writing a Ruby program that uses data from mysql queries to
I'm writing a program in Ruby that downloads a file from an RSS feed
I'm writing a ruby program that has 2 threads. One that listens on an
I'm writing a ruby program that need raw cpu power (I know ruby is
I'm writing a small program in Ruby to parse a hand history log from
I'm writing a ruby program that uses floats. I'm having trouble with the precision.
I'm writing a ruby program that will be using threads to do some work.
I'm writing a ruby program that executes some external command-line utilities. How could I
I am writing a program in Ruby which will search for strings in text
I'm writing a ruby bootstrapping script for a school project, and part of this

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.