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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:00:27+00:00 2026-05-24T04:00:27+00:00

I’d like to be able to yield within an enumerable block, in order to

  • 0

I’d like to be able to yield within an enumerable block, in order to create some boilerplate benchmarking code.

Basically I’d like to do something this (simplified):

def iterator( enumerable, &block )
  iterations = enumerable.size
  counter = 0
  enumerable.each do |item|
    counter +=1
    puts "Iterating #{counter}/#{iterations}..."
    yield
  end
end

Then I’d like to be able to use this method in order to wrap this boilerplate benchmarking code around a block I would be iterating, so that I could call something like:

# assuming foo is an enumerable collection of objects
iterator foo do
  item.slow_method
  item.mundane_method
  item.save
end

… and when this code executed I would get the following log output:

Iterating 1/1234...
Iterating 2/1234...
Iterating 3/1234...

It seems like this kind of thing must be possible, but I haven’t been able to figure out the syntax, nor what such a thing is called (in order to look it up).

The problem is I need to wrap boilerplate both OUTSIDE the enumerable object that is going to iterate, and also INSIDE the iteration block. I can pass an enumerable object in just fine, but I can’t seem to call methods on the iterated objects from within the block I pass in.

I hope this explanation makes sense, I’m having a hard time describing it. Please leave comments if you need clarification on anything, I’ll try to explain better.

  • 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-24T04:00:33+00:00Added an answer on May 24, 2026 at 4:00 am

    Ruby’s yield statement can take arguments. You would want to say

    yield item
    

    This passes the “current” item to your “outside” block.

    Hope I understood the question correctly.

    ADDENDUM

    And here is the code to show it in action:

    class Item
      def initialize(id)
        @id = id
      end
      def slow_method()
        puts "slow #@id"
      end
      def mundane_method()
        puts "mundane #@id"
      end
      def save()
        puts "save #@id"
      end
    end
    
    foo = [Item.new(100), Item.new(200), Item.new(300)]
    
    def iterator(enumerable, &block)
      iterations = enumerable.size
      counter = 0
      enumerable.each do |item|
        counter +=1
        puts "Iterating #{counter}/#{iterations}..."
        yield item
      end
    end
    
    iterator foo do |item|
      item.slow_method
      item.mundane_method
      item.save
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.