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 819831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:22:29+00:00 2026-05-15T02:22:29+00:00

I have two succeeding function with the same condition and I wonder what is

  • 0

I have two succeeding function with the same condition and I wonder what is the best way to write this? I know the one way to do this is using if (condition) … end, but I’m wondering if I can do it in one-line similar to bash, ‘[$n == $value] echo “$n” && break’.

n = 0
loop do
  puts n if n == value # puts and break is having the same condition, but
  break if n == value # can we do it in one line?
  n += 1
end
  • 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-15T02:22:29+00:00Added an answer on May 15, 2026 at 2:22 am

    One easy way is to just parenthesize the statements:

    ruby-1.9.1-p378 > 0.upto(5) do |n|
    ruby-1.9.1-p378 >     (puts n; break;) if n == 3
    ruby-1.9.1-p378 ?>    puts ">>#{n}<<"
    ruby-1.9.1-p378 ?>  end
    >>0<<
    >>1<<
    >>2<<
    3
    

    If it’s a bit much to put in parentheses, a begin-end will do the trick:

    0.upto(5) do |n|
      begin
          puts "I found a matching n!"
          puts n
          puts "And if you multiply it by 10, it is #{10*n}"
          break;
      end if n == 3
      puts "((#{n}))"
    end
    

    Output:

    ((0))
    ((1))
    ((2))
    I found a matching n!
    3
    And if you multiply it by 10, it is 30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two events: $('body').mouseup(function(e){} and $('.toggle').click(function(e){} I only want one of these to trigger.
Have two fields start and end, both are dates, I want to write a
I have two classes which need to be in same xml file. The way
We have two servers in the same network. One of them is the server
I have two JDateChoosers, One with label 'Start Date' & another with 'End Date'.I
We have two identical installations of CF9 (C:/inetpub/wwwroot/cfide/...) using the FCKeditor. One is the
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two classes (MVC view model) which inherits from one abstract base class.
I have two columns say Main and Sub . (they can be of same
I have two tables inside a database. One stores unique userNames and a unique

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.