I *LOVE* this resource (https://workingwithruby.com/wwrt/) on threading in Ruby.
However, I find the suggestion to monkey patch Enumerable here (https://workingwithruby.com/wwrt/low_level_api/) evokes anxiety.
It suggests something like this:
module Enumerable
def concurrent_each
# impl
end
end
We Rubyists *LOVE* implicit over explicit relationships. We tend to think of this as convention over configuration. Thanks, DHH.
Yet this doesn't scale well.
For apps/gems of any significant complication, the accumulation of implicitness results in cognitive overload.
What if someone *else* gets the idea to add a concurrent_each to Enumerable. That is, Enumerable, as a built-in, is akin to a global namespace.
Instead, I recommend something more like a vaguely Java-esque approach (bear with me!) of:
class ConcurrentEach
def self.using(enumerable, &block)
# use the impl supplied in the example linked above
end
end
Using this would look like:
ConcurrentEach.using(files).each { ... }
This way, we're composing instead of monkey patching or mixing in.
Try to avoid modifying code you don't own.
Or, as I've heard, second-hand, of Matz saying to a friend of mine, "Don't hurt Ruby!" 😂
#Ruby #monkeypatching #composition