4 ms·
Grepping this article there isn't one word about Refinements: https://docs.ruby-lang.org/en/3.0/doc/syntax/refinements_rdoc.html https://docs.ruby-lang.org/en/
by lamontcg 5y ago
Grepping this article there isn't one word about Refinements:
https://docs.ruby-lang.org/en/3.0/doc/syntax/refinements_rdoc.html https://docs.ruby-lang.org/en/3.0/doc/syntax/refinements_rdo...
Those are similar to C#'s Extension methods. That way consumers of the Refinement can opt-in to the extended functionality without injecting their methods globally into the class for every bit of code running on the VM.
There's a good reason that ActiveSupport as a "Monopoly on Monkeypatching" which is that nobody should be doing it any more. ActiveSupport largely gets a pass because it is pretty well defined and engineered, bits of it get pulled into core ruby, and its the 800# gorilla out there because it is rails.
You shouldn't copy that pattern, and you shouldn't want to. Use refinements instead.
- inopinatus 5y agoI like the refinements mechanism too and it is most certainly underutilized, but the lexical scope makes it an incomplete substitute, and somewhat problematic during reflection.
- chrisseaton 5y agoI'm not an expert but I believe the lexical scoping was a last-minute save by Charlie Nutter to preserve performance! Dynamic scoping of method definitions is seriously hard to optimise, and I'm not sure anyone knows how to do it. Do you really need dynamic? Why isn't lexical enough?
- inopinatus 5y agoTo clarify, I don't need it, and I'd agree that any library interface using monkeypatching could in principle be redesigned to use refinements. What I'm suggesting is in practice, I suspect it's doubtful anyone would undertake the exercise of reframing ActiveSupport (and thereby the entire Rails ecosystem) to use refinements instead of core extensions.
- lamontcg 5y agoNobody should rewrite rails and activesupport and the entire ecosystem using refinements. It would be a massively breaking change for little purpose because activesupport is so well known to the core ruby team. Nobody who is greenfielding code should copy the activesupport pattern, though, and you aren't losing anything by being told not do that, you're being shown a better way.