Module: Enumerable
- Defined in:
- lib/batch-kit/core_ext/enumerable.rb
Overview
Re-opens the Ruby standard Enumerable module to add some additional utility methods to all enumerables.
Instance Method Summary collapse
-
#concurrent_each(options = {}, &blk) ⇒ Object
Convenience function for spawning multiple threads to do a common task, driven by the contents of this enumerable.
-
#dquote ⇒ Object
Surrounds each item in the Enumerable with double quotes.
-
#squote ⇒ Object
Surrounds each item in the Enumerable with single quotes.
-
#surround(left = '"', right = left) ⇒ Object
Maps each item in the Enumerable, surrounding it with the
leftandrightstrings.
Instance Method Details
#concurrent_each(options = {}, &blk) ⇒ Object
Convenience function for spawning multiple threads to do a common task, driven by the contents of this enumerable. Each entry in self will be be yielded to a new thread, which will then call the supplied block with the element.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/batch-kit/core_ext/enumerable.rb', line 43 def concurrent_each( = {}, &blk) if self.count < 2 self.each(&blk) else abort_opt = .fetch(:abort_on_exception, true) Thread.abort_on_exception = abort_opt # Push items onto a queue from which work items can be removed by # threads in the pool queue = Queue.new self.each{ |it| queue << it } # Setup thread pool to iterate over work queue thread_count = .fetch(:threads, [4, self.count].min) threads = [] # Launch each worker thread, which loops extracting work items from # the queue until it is empty (0...thread_count).each do |i| threads << Thread.new do begin while work_item = queue.pop(true) if abort_opt # Raise exception on main thread begin yield work_item rescue Exception => ex Thread.main.raise ex end else # Exceptions will be picked up below when main thread joins yield work_item end end rescue ThreadError # Work queue is empty, so exit loop end end end # Now wait for all threads in pool to complete ex = nil threads.each do |th| begin th.join rescue Exception => t ex = t unless ex end end raise ex if ex end end |
#dquote ⇒ Object
Surrounds each item in the Enumerable with double quotes.
26 27 28 |
# File 'lib/batch-kit/core_ext/enumerable.rb', line 26 def dquote self.surround('"') end |
#squote ⇒ Object
Surrounds each item in the Enumerable with single quotes.
20 21 22 |
# File 'lib/batch-kit/core_ext/enumerable.rb', line 20 def squote self.surround("'") end |
#surround(left = '"', right = left) ⇒ Object
Maps each item in the Enumerable, surrounding it with the left and right strings. If right is not specified, it is set to the same string as left, which in turn is defaulted to the double-quote character.
14 15 16 |
# File 'lib/batch-kit/core_ext/enumerable.rb', line 14 def surround(left = '"', right = left) self.map{ |item| "#{left}#{item}#{right}" } end |