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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:26+00:00 2026-05-27T10:32:26+00:00

I am developing a Ruby on Rails app. My question is more about Ruby

  • 0

I am developing a Ruby on Rails app. My question is more about Ruby syntax.

I have a model class with a class method self.check:

class Cars < ActiveRecord::Base
  ...
  def self.check(name)
     self.all.each do |car|
          #if result is true, break out from the each block, and return the car how to...
          result = SOME_CONDITION_MEET?(car) #not related with database
     end

     puts "outside the each block."
  end
end

I would like to stop/break out from the each block once the result is true (that’s break the each block if car.name is the same as the name parameter once) AND return the car which cause the true result. How to break out in Ruby code?

  • 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-27T10:32:27+00:00Added an answer on May 27, 2026 at 10:32 am

    You can break with the break keyword. For example

    [1,2,3].each do |i|
      puts i
      break
    end
    

    will output 1. Or if you want to directly return the value, use return.

    Since you updated the question, here the code:

    class Car < ActiveRecord::Base
      # …
    
      def self.check(name)
        self.all.each do |car|
          return car if some_condition_met?(car)
        end
    
        puts "outside the each block."
      end
    end
    

    Though you can also use Array#detect or Array#any? for that purpose.

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

Sidebar

Related Questions

I am developing a Ruby on Rails app and I have a model called
I am developing a Ruby on Rails website and I have an architectural question
I have a command line Ruby app I'm developing and I want to allow
I am developing a Ruby on Rails app and attempting to deploy it with
I have started developing a new Rails app on my server using RVM, Rails
I am developing a Ruby on Rails application with multilingual support. Now I have
I'm developing a web app in Ruby-On-Rails. It heavily uses JavaScript. The problem is,
I am developing a webapp (well trying to) in Ruby on Rails. I have
I'm developing a ruby on rails app and I want to be able to
i am developing a app and have been for a while using Rails 2.3.4

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.