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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:38:34+00:00 2026-05-13T07:38:34+00:00

I need a method that takes a block, and performs something similar to an

  • 0

I need a method that takes a block, and performs something similar to an around_each filter for every method within the block.

For instance:

def method_that_takes_block
  (@threads ||= Array.new) << Thread.new {yield if block.given?}
end

method_that_takes_a_block do
  method_one
  method_two
  method_three
end

In this instance I would like my method that takes a block to Thread each method within the block and pushes that thread to the @threads array. Essentially I’m just looking for a DRY way to wrap a thread around every method called within a block.

  • 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-13T07:38:34+00:00Added an answer on May 13, 2026 at 7:38 am

    You can’t directly wrap a thread around each statement in the block body, if they can be arbitrary statements; there’s no way to get that sort of control over the execution of a block body in Ruby. If you can restrict what goes in the block body, you have some more flexibility.

    If each of the statements you are executing is simply a method call, as you imply in your example, you can use instance_exec to execute that block on a proxy object, which uses method_missing to spawn a new thread and then forward the method call on to the real object (or do whatever wrapper you’re interested in; for the sake of example, I’ll just wrap with some print statements):

    class Proxy
      def initialize obj
        @obj = obj
      end
      def method_missing method, *args
        puts "<wrapper>"
        @obj.send method, *args
        puts "</wrapper>"
      end
    end
    
    class MethodWrapper
      def tell_me_a_joke
        puts "Knock, knock?"
      end
      def whos_there
        puts "Orange"
      end
      def orange_who
        puts "Orange you glad I didnt say banana?"
      end
    
      def wrap_around &blk
        Proxy.new(self).instance_exec &blk
      end
    end
    

    And here’s how you can use it:

    >> MethodWrapper.new.wrap_around { tell_me_a_joke; whos_there; orange_who }
    <wrapper>
    Knock, knock?
    </wrapper>
    <wrapper>
    Orange
    </wrapper>
    <wrapper>
    Orange you glad I didnt say banana?
    </wrapper>
    => nil
    

    The previous follows the pattern that you gave in your question, but it’s less than ideal as only methods that are forwarded to the underlying object get wrapped:

    >> MethodWrapper.new.wrap_around { tell_me_a_joke; puts "something" }
    <wrapper>
    Knock, knock?
    </wrapper>
    something
    => nil
    

    You could instead just use instance_exec directly, and call the a wrapper method that takes a block, to get almost the same effect, though slightly less DRY as you need to call your wrapper method each time:

    class SimpleWrapper
      def tell_me_a_joke
        puts "Knock, knock?"
      end
      def whos_there
        puts "Interrupting cow"
      end
      def interrupting_co
        puts "Moooooo!"
      end
    
      def wrap
        puts "<wrap>"
        yield
        puts "</wrap>"
      end
    end
    

    And in use:

    >> SimpleWrapper.new.instance_exec do
         wrap { tell_me_a_joke }
         wrap { whos_there }
         wrap { interrupting_co }
         wrap { puts "Something" }
       end
    <wrap>
    Knock, knock?
    </wrap>
    <wrap>
    Interrupting cow
    </wrap>
    <wrap>
    Moooooo!
    </wrap>
    <wrap>
    Something
    </wrap>
    => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a method that takes a linkedlist as a parameter and return true
I know downcasting like this won't work. I need a method that WILL work.
I've a need to add method that will calculate a weighted sum of worker
I need to call a method that accepts a stream argument. The method loads
I need a tool or method that allows the transfer of data and automatically
I have a JavaScript method that I need to run on one of my
I need assistance finding a delivery method that best fulfills the following requirements: We
I need to write a unit test for a method that will print a
I need a hasEvents() method like var someBool = hasEvents($(#myelement)); that returns true if
I need a Regex that will match a java method declaration. I have come

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.