10 ms·
Responsible Monkeypatching in Ruby
- develop7 5y agoI wonder why this otherwise thorough post doesn't mention almost decade-old feature called "refinements" [1]? 1: https://ruby-doc.org/core-2.5.3/doc/syntax/refinements_rdoc.html https://ruby-doc.org/core-2.5.3/doc/syntax/refinements_rdoc....
- eloisius 5y agoNot sure why the author chose not to, but personally I’ve avoided use of refinements because of the weird, stateful way that it changes the implementation of a class. It’s too confusing to scan the code quickly and know whether a particular class has a refinement activated in a particular scope. Ideally don’t monkey patch things, wrap them in your own class that decorates a stdlib class. If you can’t do that, my personal preference is to have the monkey patched class be consistently monkey patched on all scopes.
- Lio 5y agoI don't know the authors reason but I've personally always avoided refinements after first reading about their performance hit for JRuby code[0]. I read somewhere recently that even for MRI ruby you can take a 40% hit when using refinements (but please take that with a big pinch of salt as I can't find the source right now). 0. http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/33487 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/...
- byroot 5y agoThe source is me: https://gist.github.com/casperisfine/1c46f05cccfa945cd156f445f0d3d6fa https://gist.github.com/casperisfine/1c46f05cccfa945cd156f44... Note that this overhead apply as soon as a refinement is defined for a method regardless of wether it's ever active. That 40% figure is for an empty method. So for "big" methods that are infrequently called it's probably fine, but it should really be avoided in hotspots.
- Lio 5y agoAha! Excellent, thank you. For me refinements are a bit like meta-programming; I'm glad they exist but I think there's other ways to get the same result which are easier to live with.
- petepete 5y agoI use refinements in a couple of my projects, they definitely feel like the right approach, especially in a library where you don't want unintended consequences. Admittedly they're a bit of a mystery to lots of devs.
- pankajdoharey 5y agoAbsolutely, refinements are the most legit way of monkeypatching. I could almost bet the author doesnt know about it, had he known there is no way anyone would forego mentioning it when it was specifically introduced to address the title of the post.
- nerdponx 5y agoCan someone help me understand why anyone would ever rely on monkeypatching in non-test code? Python is just as dynamic as Ruby in a lot of ways. But in Python, overwriting an existing and important/fundamental/ubiquitous method would be an absolute last resort when all other sensible approaches have failed. And in that case you would at least have the sense to save the original method somewhere under a different name. What makes such a horrifying idea so apparently not-horrifying in Ruby? The ability to "reopen" class? None of the examples in the blog post seemed like such dire circumstances as to require it. Maybe replacing a buggy method while you wait for a PR and new release is a valid use case. But the failure mode is also particularly nasty.
- joelbluminator 5y agoThe best example I can think of is Rails' ActiveSupport. The changes it brought to Ruby aren't fundamental in any way but they are convenient. For example asking whether a variable is present? (truthy, so empty str and empty [] should return false) is not possible with plain Ruby but is possible in Rails. It's stuff web developers need and makes things simpler. This is done by monkeypatching. Another example is if you're working on a library that has some bug but you can't quite yet jump to the next version.
- eloisius 5y agoMy favorite ActiveSupport monkey patching is on number classes. Things like 2.weeks.from_now are just too useful to scorn monkey patching on some programming ideological reasons.
- joelbluminator 5y agoOh I love these yes please!
- Groxx 5y agoI wish every language had open classes for this reason. It makes ruby such a joy to write in. Something like Kotlin's class extensions might be a happier middle ground for many languages though, since that way you only get what you pulled in, in any given file. Yes, of course you can do horrific things with it. But the ruby community has largely been absolutely stellar about not doing that - most keep insanities tightly bounded, where they can be used to make pleasant and error-resistant APIs. And even when they're not (which is usually self-inflicted by internal code), the language is flexible enough to let you unfuck just about any fuckery that you run across, if needed.
- deleted 5y ago[deleted]
- ysavir 5y agoThere is no such thing as "responsible monkeypatching". Even if it's fixing a bug in the source code or annotating, as the author suggests are practical uses. Writing good code is about clarity and working within maintained expectations. People reading your code should be able to know what it does. If someone joins your team and knows that Method X in library Y is bugged, they will expect it to be bugged in your codebase as well, and reasonably so. The fact that you monkeypatched it is not something that they will be aware of, and can potentially cause bugs in their code as a consequence, costing them time and patience. A better approach is to use a wrapper instead of monkeypatching, or inherit from the bugged class and overwrite the bugged method. You get the same results, but anyone reading your code, with or without prior knowledge of how your code works, will: 1. Have immediate understanding that you've altered the source code in some way 2. Have a crumb trail leading them back to your custom implementation
- BiteCode_dev 5y agoExactly, when the article used their example of bug found in production, I knew what would happen before the reveal, because it's what always happen. Damn it, you got a language with namespaces, why do you think that is ?