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.

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:

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.

  1. What does refine inside a module do?

    Answer

    Defines methods on a class for code that activates the module with using

    The 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.

  2. What does this program print? module Doubler refine Integer do def twice = self * 2 end end using Doubler p 21.twice

    Answer

    42

    The refinement adds twice to Integer for this file, so the call doubles its receiver.

  3. 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 end

    Answer

    NoMethodError

    No code activated the module, so String has no hi and the call raises NoMethodError.

  4. 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" end

    Answer

    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.

  5. 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".tag

    Answer

    B

    Each using line activates a module, and the last one activated wins for a method both define.

  6. 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.

  7. 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 end

    Answer

    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.

  8. A gem defines module Shout with a refinement of String. What does a file do to call the refined method?

    Answer

    It requires the gem and calls using Shout before the call

    A refinement is inert until a file activates it, which is why the caller writes using and the gem cannot force the method on anyone.

  9. lib/shout.rb calls using Shout at its top level. main.rb requires 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.

  10. Two files refine String#title differently 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