Skip to content

Instantly share code, notes, and snippets.

@joejag
Created August 14, 2014 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joejag/f530a3814df6dfa8e7d9 to your computer and use it in GitHub Desktop.
Save joejag/f530a3814df6dfa8e7d9 to your computer and use it in GitHub Desktop.
Parallel each method for ruby
class Array
def peach(&block)
threads = []
self.each do |item|
threads << Thread.new do
block.call(item)
end
end
threads.each {|t| t.join }
end
end
@nroose
Copy link

nroose commented Jul 16, 2018

Good stuff!

I am using a version that is slightly more concise:

class Array
  def peach(&block)
    threads = self.map do |item|
      Thread.new do
        block.call(item)
      end
    end
    threads.each(&:join)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment