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

  • Home
  • SEARCH
  • 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 8342391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:45:27+00:00 2026-06-09T05:45:27+00:00

I use this pattern repeatedly: pb= ProgressBar.new(blah_map, wtf.count) newresult= cache(blah_map) do result.map{ |r| pb.inc;

  • 0

I use this pattern repeatedly:

pb= ProgressBar.new("blah_map", wtf.count)
newresult= cache("blah_map") do 
  result.map{ |r| pb.inc; { blah: r[:x] } }
end
pb.finish

or

pb= ProgressBar.new("blah_group", wtf.count)
newresult= cache("blah_group") do 
  result.group_by{ |r| pb.inc; "#{r[:x]}&#{r[:y]}" }
end
pb.finish

So naturally I would like to be able to do

def progress_cache(name, count, &block)
  pb= ProgressBar.new(name, count)
  inject_pb_inc(block) # how??
  # do some caching with yield if cache doesn't exist, don't mind this
  pb.finish
end

And use it as such:

newresult= progress_cache("lol", result.count) do 
  result.map do |r| 
    # progress_cache magically inserted a pb.inc here for pretty animation!  
    r[:omg] 
  end 
end

The question is, how to inject the pb.inc call into the block (map, group_by etc) inside progress_cache block?

Edit: rephrased the question

  • 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-06-09T05:45:29+00:00Added an answer on June 9, 2026 at 5:45 am

    There are a few ways to accomplish this, with various tradeoffs in terms of expressiveness:

    1. Send the progress bar as a block param

      def progress_cache(name, count &block)
        pb = ProgressBar.new(name, count)
        result = block.call(pb)
        pb.finish
        result
      end
      

      and use it like

      newresult = progress_cache("lol", result.count) do |pb|
        result.map{|r| pb.inc; r[:omg]}
      end
      
    2. Create a new map function that automatically increments the progress bar (you could overwrite result.map directly, or provide result.map_with_progress, but I’ll leave that up to you)

      def map_with_progress(container, name, &block)
        pb = ProgressBar.new(name, container.count)
        result = container.map{|obj| block.call(obj)}
        pb.finish
        result
      end
      

      and then use it like

      newresult = map_with_progress(result, "lol") { |r| r[:omg] }
      

      of course, since you’re doing both a map and a group_by, you’d have to have two helper methods here, which might start getting messy.

    3. Use a higher-order function

      def function_with_progress(obj, func_name, name, count, &block)
        pb = ProgressBar.new(name, count)
        result = obj.__send__(func_name) do |param|
          pb.inc
          block.call(param)
        end
        pb.finish
        result
      end
      

      and then use it like

      newresult = function_with_progress(result, "map", "lol", result.count) do |r|
        r[:omg]
      end
      

      but I wouldn’t recommend this approach as it’s too abstract. It would work in a functional language like javascript or clojure, but I don’t think it’s appropriate for ruby.

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

Sidebar

Related Questions

I want to use this pattern: SqlCommand com = new SqlCommand(sql, con); com.CommandType =
In answering this question , it got me thinking... I often use this pattern:
I use this pattern to test for undefined and null values in ActionScript/Flex :
I would like to use this pattern to enable dependency injection in my code.
I use this pattern all over the place to grab data from the database
I often use this pattern for easy deployment of websites: I have a bare
Hi all whenever I use the synchronized statement, I often use this pattern: private
I had a regex, like so: (?<one-1>cat)|(?<two-2>dog)|(?<three-3>mouse)|(?<four-4>fish) When I tried to use this pattern
I use this code: static Pattern escaper = Pattern.compile(([^a-zA-z0-9])); public static String escapeRE(String str)
I normally use this pattern to iterate over object properties: for(var property in object)

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.