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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:05:33+00:00 2026-05-18T07:05:33+00:00

I’m trying to define a DSL where rules (for the sake of this example,

  • 0

I’m trying to define a DSL where rules (for the sake of this example, the rules define whether something is “good” or “bad”) are specified in a block in Ruby. The following is a (grossly simplified) version of what I want to do:

def test_block
  # Lots of other code
  is_good = yield   # ... should give me true or false
  # Lots of other code
end

test_block do
  good if some_condition
  good if some_other_condition
  bad
end

Is there any way I can define methods good and bad that make the block break? In the above example, I want to:

  • check if some_condition is true, and if it is, break out of the block and have it return true
  • check if some_other_condition is true, and if it is, break out of the block and have it return true
  • return false from the block unconditionally if we’re still in it

i.e. I want to make the above code behave as if I had written the block like so:

result = test_block do
  break true if some_condition
  break true if some_other_condition
  break false
end

Putting break in the definition of the good/bad method obviously doesn’t work. Is there some other way of achieving my desired result or should I think about some entirely different way of going about this?

  • 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-18T07:05:33+00:00Added an answer on May 18, 2026 at 7:05 am

    You could raise an exception in the block and catch that exception.

    module Tester
      class Breaker < Exception; end
      class GoodBreak < Breaker; end
      class BaadBreak < Breaker; end
    end
    
    def test_block(name)
      begin
        yield
      rescue Tester::Breaker=>e
        case e
          when Tester::GoodBreak then puts "All is well with #{name}"
          when Tester::BaadBreak then puts "BAD STUFF WITH #{name}"
          else raise
        end
      end
    end
    
    def good; raise Tester::GoodBreak; end
    def bad;  raise Tester::BaadBreak; end
    
    test_block('early out') do
      good if true
      good if puts("NEVER SEE THIS") || true
      bad
    end
    
    test_block('simple pass') do
      good if false
      good if puts("SEE THIS FROM PASS TEST") || true
      bad
    end
    
    test_block('final fail') do
      good if false
      good if puts("SEE THIS BUT PUTS IS NIL")
      bad
    end
    
    #=> All is well with early out
    #=> SEE THIS FROM PASS TEST
    #=> All is well with simple pass
    #=> SEE THIS BUT PUTS IS NIL
    #=> BAD STUFF WITH final fail
    

    Here’s another example using throw/catch (thanks @jleedev!) instead of raise/rescue (updated to pass a return value along):

    def test_block(name)
      result = catch(:good){ catch(:bad){ yield } }
      puts "Testing #{name} yielded '#{result}'", ""
    end
    
    def good; throw :good, :good; end
    def bad;  throw :bad,  :bad;  end
    
    test_block('early out') do
      good if true
      good if puts("NEVER SEE THIS") || true
      bad
    end
    
    test_block('simple pass') do
      good if false
      good if puts("SEE THIS FROM PASS TEST") || true
      bad
    end
    
    test_block('final fail') do
      good if false
      good if puts("SEE THIS BUT PUTS IS NIL")
      bad
    end
    
    #=> Testing early out yielded 'good'
    #=> 
    #=> SEE THIS FROM PASS TEST
    #=> Testing simple pass yielded 'good'
    #=> 
    #=> SEE THIS BUT PUTS IS NIL
    #=> Testing final fail yielded 'bad'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

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.