Refinements
refine and using
A refinement is a monkey patch with a scope. refine defines methods on a class inside a module, and using switches the module on for the rest of the file:
module Shout refine String do def hi = "hi #{self}" end end using Shout p "x".hi
ruby refine.rb
"hi x"
The call is a normal method call; the refinement is the reason it resolves. Outside the file, String has no hi, which is the whole point.
The scope is the file
using is lexical: it applies from the line to the end of the file, and to blocks defined there. A class defined before the using does not see the refinement, even when it is called afterwards:
module Shout refine String do def hi = "hi #{self}" end end class Plain def call(string) = string.hi end using Shout p "x".hi begin Plain.new.call("y") rescue NoMethodError puts "class: no hi" end
ruby scope.rb
"hi x"
class: no hi
The method body was compiled where hi did not exist, and nothing later adds it to that scope. That is the behaviour that makes a refinement safe: a library cannot change a method for code that never asked.
Why it does not leak
A monkey patch reopens the class for the whole process. A refinement is a separate lookup table attached to a scope, so two files can refine String differently and neither sees the other.
- Opt in. A file says using or it gets the standard library.
- Testable. The file that refines can be tested on its own, and the rest of the suite is unaffected.
- Composable. Two refinements on the same class are ordered by the using lines, like includes.
When to use it
Rails, RSpec and many gems still ship a monkey patch, because a refinement in a gem is useless to the code that requires it unless that code also writes using. The honest uses are:
- A test helper. A method that makes assertions read better, visible only in the test files.
- A compatibility shim. One file that adapts an old API, kept out of the rest of the process.
- A local experiment. A method tried in a script without touching the library for everyone.
What to watch for
using at the top level of a required file applies
to that file, not to the file that required it. A helper meant for the
caller has to be applied by the caller, which is why a gem rarely uses
one for its public API.
A method defined before the using line cannot see
the refinement, even when the refinement is active by the time the method
runs. The rule is the same as the class above, and the error is a
NoMethodError at the call, not at the definition.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
refineinside a module do?Answer
Defines methods on a class for code that activates the module with
usingThe methods live in the module and reach only the lookup of code that activates it, which is what keeps a refinement out of the rest of the program.
-
What does this program print?
module Doubler refine Integer do def twice = self * 2 end end using Doubler p 21.twiceAnswer
42
The refinement adds twice to Integer for this file, so the call doubles its receiver.
-
What does this program print?
module Shout refine String do def hi = "hi #{self}" end end begin p "x".hi rescue NoMethodError => e puts e.class endAnswer
NoMethodError
No code activated the module, so String has no hi and the call raises NoMethodError.
-
What does this program print?
module Shout refine String do def hi = "hi #{self}" end end class Plain def call(text) = text.hi end using Shout puts "ok" begin Plain.new.call("y") rescue NoMethodError puts "no hi in the class" endAnswer
ok / no hi in the class
call was written before the using line, so its body never sees the refinement and its call raises NoMethodError.
-
What does this program print?
module A refine String do def tag = "A" end end module B refine String do def tag = "B" end end using A using B puts "x".tagAnswer
B
Each using line activates a module, and the last one activated wins for a method both define.
-
What does this program print?
module Shout refine String do def hi = "hi #{self}" end end using Shout puts "x".respond_to?(:hi) puts String.instance_methods(false).include?(:hi)Answer
true / false
The refinement is active for this file, so respond_to? answers for it, but the class itself still has no such instance method.
-
What does this program print?
module Shout refine String do def hi = "hi #{self}" end end def shout using Shout "x".hi end begin shout rescue RuntimeError => e puts e.message endAnswer
main.using is permitted only at toplevel
using activates a refinement for a lexical scope, so it is allowed at the top level of a file or in a module body, never inside a method.
-
A gem defines
module Shoutwith a refinement ofString. What does a file do to call the refined method?Answer
It requires the gem and calls
using Shoutbefore the callA refinement is inert until a file activates it, which is why the caller writes using and the gem cannot force the method on anyone.
-
lib/shout.rbcallsusing Shoutat its top level.main.rbrequires that file and then calls"x".hi. What happens?Answer
The call raises NoMethodError, because the using in the library applies to the library file only
A refinement is scoped to the file, module or class that activates it, so the caller that wants it has to write its own using line.
-
Two files refine
String#titledifferently and one program loads both. What does the code of each file see?Answer
Its own refinement, because each activation belongs to its file
Each file carries the activation it wrote, so the two refinements never meet and neither library changes the other.
Article author: Arthur Isaev