3 ms·
As for refinements, I have never used them, never seen them used, and I don't even really remember what they are. I think they were to save us from
by JohnBooty 8y ago
As for refinements, I have never used them, never
seen them used, and I don't even really remember
what they are. I think they were to save us from
the evils of monkey patching.
They're pretty cool. IMHO they could be considered a best practice whenever you're reopening a class, whether it's for the purpose of monkey patching an existing method or adding your own.
Rather than overriding, say, `String#upcase` on a global basis you could override it in some reduced scope, like within some specific class. This is arguably not ideal, but I would say that it is more explicit and less dangerous than a global monkey patch.
Or, if you have a use case for adding a bunch of new methods to `String`, you can go ahead and add that `String#decorate_with_random_emoji` method you've always dreamed of - but only within particular scopes, such as a particular Rails presenter or something. I think this is often better than polluting the `String` class globally, or having some utility class full of methods like `MyAwesomeStringUtils.decorate_with_random_emoji(some_string)`.
In most cases I would say that these refinements belong in a mixin that classes can explicitly opt into -- `include my_string_refinements` or whatever.
- jacobsenscott 8y agoThanks. Maybe I'll take another look at them.