If you’ve been following Ruby developments for the past couple of years, chances are you’ve heard about refinements. You may have heard that they are controversial, confusing, or even “broken”.
It’s true that refinements had some growing pains in their early, experimental versions. But having spent some time exploring the feature as it now exists in Ruby 2.1 and onward, I think they’re a valuable addition to the language. In a language where any class can be re-opened at any time, refinements provide a nice mix of language malleability tempered by locality and visibility.
Back in October I released a video to my RubyTapas subscribers which set out to introduce and demystify this powerful new ability. Today I’m releasing it for free in hopes that it will help clear up some of the confusion around refinements.
Here’s the video:
By the way, if you want to see an example of a real-world use of refinements, check out the Sequel core_refinements library. Sequel has a set of core class extensions which are traditionally added globally. But with refinements, it’s possible to make use of these convenience extensions without either forcing or requiring them to be globally available. This is particularly handy within gems, where we might want the ease of use of the extension methods, but we don’t want to force global extensions on our library clients.
If you prefer reading over watching, the script is below. And remember, if you like this, please consider subscribing and supporting the creation of more videos like this!