2013-01-23 19:40:49 -05:00
|
|
|
= Refinements
|
|
|
|
|
|
|
|
Due to Ruby's open classes you can redefine or add functionality to existing
|
|
|
|
classes. This is called a "monkey patch". Unfortunately the scope of such
|
|
|
|
changes is global. All users of the monkey-patched class see the same
|
|
|
|
changes. This can cause unintended side-effects or breakage of programs.
|
|
|
|
|
|
|
|
Refinements are designed to reduce the impact of monkey patching on other
|
|
|
|
users of the monkey-patched class. Refinements provide a way to extend a
|
2018-12-24 16:14:38 -05:00
|
|
|
class locally. Refinements can modify both classes and modules.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
Here is a basic refinement:
|
|
|
|
|
|
|
|
class C
|
|
|
|
def foo
|
2013-05-05 07:49:53 -04:00
|
|
|
puts "C#foo"
|
2013-01-23 19:40:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module M
|
|
|
|
refine C do
|
|
|
|
def foo
|
|
|
|
puts "C#foo in M"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
First, a class +C+ is defined. Next a refinement for +C+ is created using
|
2018-12-24 16:14:38 -05:00
|
|
|
Module#refine.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
Module#refine creates an anonymous module that contains the changes or
|
|
|
|
refinements to the class (+C+ in the example). +self+ in the refine block is
|
|
|
|
this anonymous module similar to Module#module_eval.
|
|
|
|
|
|
|
|
Activate the refinement with #using:
|
|
|
|
|
|
|
|
using M
|
|
|
|
|
2014-01-21 07:00:08 -05:00
|
|
|
c = C.new
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
c.foo # prints "C#foo in M"
|
|
|
|
|
|
|
|
== Scope
|
|
|
|
|
2015-11-17 20:08:19 -05:00
|
|
|
You may activate refinements at top-level, and inside classes and modules.
|
|
|
|
You may not activate refinements in method scope. Refinements are activated
|
|
|
|
until the end of the current class or module definition, or until the end of
|
|
|
|
the current file if used at the top-level.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
2015-11-17 20:08:19 -05:00
|
|
|
You may activate refinements in a string passed to Kernel#eval. Refinements
|
2017-06-22 20:29:45 -04:00
|
|
|
are active until the end of the eval string.
|
2015-11-17 20:08:19 -05:00
|
|
|
|
|
|
|
Refinements are lexical in scope. Refinements are only active within a scope
|
2017-06-22 20:29:45 -04:00
|
|
|
after the call to +using+. Any code before the +using+ statement will not have the
|
2015-11-17 20:08:19 -05:00
|
|
|
refinement activated.
|
|
|
|
|
2017-06-22 20:29:45 -04:00
|
|
|
When control is transferred outside the scope, the refinement is deactivated.
|
2015-11-17 20:08:19 -05:00
|
|
|
This means that if you require or load a file or call a method that is defined
|
|
|
|
outside the current scope the refinement will be deactivated:
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
class C
|
|
|
|
end
|
|
|
|
|
|
|
|
module M
|
|
|
|
refine C do
|
|
|
|
def foo
|
|
|
|
puts "C#foo in M"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_foo(x)
|
|
|
|
x.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
using M
|
|
|
|
|
|
|
|
x = C.new
|
|
|
|
x.foo # prints "C#foo in M"
|
|
|
|
call_foo(x) #=> raises NoMethodError
|
|
|
|
|
2017-06-22 20:29:45 -04:00
|
|
|
If a method is defined in a scope where a refinement is active, the refinement
|
2013-01-23 19:40:49 -05:00
|
|
|
will be active when the method is called. This example spans multiple files:
|
|
|
|
|
|
|
|
c.rb:
|
|
|
|
|
|
|
|
class C
|
|
|
|
end
|
|
|
|
|
|
|
|
m.rb:
|
|
|
|
|
|
|
|
require "c"
|
|
|
|
|
|
|
|
module M
|
|
|
|
refine C do
|
|
|
|
def foo
|
|
|
|
puts "C#foo in M"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
m_user.rb:
|
|
|
|
|
|
|
|
require "m"
|
|
|
|
|
|
|
|
using M
|
|
|
|
|
|
|
|
class MUser
|
|
|
|
def call_foo(x)
|
|
|
|
x.foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main.rb:
|
|
|
|
|
|
|
|
require "m_user"
|
|
|
|
|
|
|
|
x = C.new
|
|
|
|
m_user = MUser.new
|
|
|
|
m_user.call_foo(x) # prints "C#foo in M"
|
|
|
|
x.foo #=> raises NoMethodError
|
|
|
|
|
|
|
|
Since the refinement +M+ is active in <code>m_user.rb</code> where
|
|
|
|
<code>MUser#call_foo</code> is defined it is also active when
|
|
|
|
<code>main.rb</code> calls +call_foo+.
|
|
|
|
|
|
|
|
Since #using is a method, refinements are only active when it is called. Here
|
|
|
|
are examples of where a refinement +M+ is and is not active.
|
|
|
|
|
|
|
|
In a file:
|
|
|
|
|
|
|
|
# not activated here
|
|
|
|
using M
|
|
|
|
# activated here
|
|
|
|
class Foo
|
|
|
|
# activated here
|
|
|
|
def foo
|
|
|
|
# activated here
|
|
|
|
end
|
|
|
|
# activated here
|
|
|
|
end
|
|
|
|
# activated here
|
|
|
|
|
2015-11-17 20:08:19 -05:00
|
|
|
In a class:
|
|
|
|
|
|
|
|
# not activated here
|
|
|
|
class Foo
|
|
|
|
# not activated here
|
|
|
|
def foo
|
|
|
|
# not activated here
|
|
|
|
end
|
|
|
|
using M
|
|
|
|
# activated here
|
|
|
|
def bar
|
|
|
|
# activated here
|
|
|
|
end
|
|
|
|
# activated here
|
|
|
|
end
|
|
|
|
# not activated here
|
|
|
|
|
2017-06-22 20:29:45 -04:00
|
|
|
Note that the refinements in +M+ are *not* activated automatically if the class
|
|
|
|
+Foo+ is reopened later.
|
2015-11-17 20:08:19 -05:00
|
|
|
|
2013-01-23 19:40:49 -05:00
|
|
|
In eval:
|
|
|
|
|
|
|
|
# not activated here
|
|
|
|
eval <<EOF
|
|
|
|
# not activated here
|
|
|
|
using M
|
|
|
|
# activated here
|
|
|
|
EOF
|
|
|
|
# not activated here
|
|
|
|
|
|
|
|
When not evaluated:
|
|
|
|
|
|
|
|
# not activated here
|
|
|
|
if false
|
|
|
|
using M
|
|
|
|
end
|
|
|
|
# not activated here
|
|
|
|
|
2017-06-22 20:29:45 -04:00
|
|
|
When defining multiple refinements in the same module inside multiple +refine+ blocks,
|
2017-11-07 15:04:58 -05:00
|
|
|
all refinements from the same module are active when a refined method
|
|
|
|
(any of the +to_json+ methods from the example below) is called:
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
module ToJSON
|
|
|
|
refine Integer do
|
|
|
|
def to_json
|
|
|
|
to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refine Array do
|
|
|
|
def to_json
|
|
|
|
"[" + map { |i| i.to_json }.join(",") + "]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refine Hash do
|
|
|
|
def to_json
|
|
|
|
"{" + map { |k, v| k.to_s.dump + ":" + v.to_json }.join(",") + "}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
using ToJSON
|
|
|
|
|
|
|
|
p [{1=>2}, {3=>4}].to_json # prints "[{\"1\":2},{\"3\":4}]"
|
|
|
|
|
2013-07-02 04:24:26 -04:00
|
|
|
|
2013-01-23 19:40:49 -05:00
|
|
|
== Method Lookup
|
|
|
|
|
2013-01-24 02:54:45 -05:00
|
|
|
When looking up a method for an instance of class +C+ Ruby checks:
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
* If refinements are active for +C+, in the reverse order they were activated:
|
|
|
|
* The prepended modules from the refinement for +C+
|
|
|
|
* The refinement for +C+
|
|
|
|
* The included modules from the refinement for +C+
|
|
|
|
* The prepended modules of +C+
|
|
|
|
* +C+
|
|
|
|
* The included modules of +C+
|
|
|
|
|
|
|
|
If no method was found at any point this repeats with the superclass of +C+.
|
|
|
|
|
|
|
|
Note that methods in a subclass have priority over refinements in a
|
|
|
|
superclass. For example, if the method <code>/</code> is defined in a
|
2017-06-22 20:29:45 -04:00
|
|
|
refinement for Numeric <code>1 / 2</code> invokes the original Integer#/
|
|
|
|
because Integer is a subclass of Numeric and is searched before the refinements
|
2017-11-07 15:04:58 -05:00
|
|
|
for the superclass Numeric. Since the method <code>/</code> is also present
|
|
|
|
in child +Integer+, the method lookup does not move up to the superclass.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
2017-06-22 20:29:45 -04:00
|
|
|
However, if a method +foo+ is defined on Numeric in a refinement, <code>1.foo</code>
|
|
|
|
invokes that method since +foo+ does not exist on Integer.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
== +super+
|
|
|
|
|
|
|
|
When +super+ is invoked method lookup checks:
|
|
|
|
|
|
|
|
* The included modules of the current class. Note that the current class may
|
|
|
|
be a refinement.
|
|
|
|
* If the current class is a refinement, the method lookup proceeds as in the
|
|
|
|
Method Lookup section above.
|
|
|
|
* If the current class has a direct superclass, the method proceeds as in the
|
|
|
|
Method Lookup section above using the superclass.
|
|
|
|
|
|
|
|
Note that +super+ in a method of a refinement invokes the method in the
|
|
|
|
refined class even if there is another refinement which has been activated in
|
|
|
|
the same context.
|
|
|
|
|
2019-01-10 12:28:13 -05:00
|
|
|
== Methods Introspection
|
2013-01-23 19:40:49 -05:00
|
|
|
|
2019-01-10 12:28:13 -05:00
|
|
|
When using introspection methods such as Kernel#method or Kernel#methods refinements are not honored.
|
2013-01-23 19:40:49 -05:00
|
|
|
|
|
|
|
This behavior may be changed in the future.
|
|
|
|
|
2017-12-03 03:35:44 -05:00
|
|
|
== Refinement inheritance by Module#include
|
|
|
|
|
2017-12-08 17:51:42 -05:00
|
|
|
When a module X is included into a module Y, Y inherits refinements from X.
|
2017-12-03 03:35:44 -05:00
|
|
|
|
2018-01-10 09:13:06 -05:00
|
|
|
For example, C inherits refinements from A and B in the following code:
|
2017-12-03 03:35:44 -05:00
|
|
|
|
|
|
|
module A
|
|
|
|
refine X do ... end
|
|
|
|
refine Y do ... end
|
|
|
|
end
|
|
|
|
module B
|
|
|
|
refine Z do ... end
|
|
|
|
end
|
|
|
|
module C
|
|
|
|
include A
|
|
|
|
include B
|
|
|
|
end
|
|
|
|
|
|
|
|
using C
|
|
|
|
# Refinements in A and B are activated here.
|
|
|
|
|
2017-12-08 17:51:42 -05:00
|
|
|
Refinements in descendants have higher precedence than those of ancestors.
|
2017-12-03 03:35:44 -05:00
|
|
|
|
2013-01-23 19:40:49 -05:00
|
|
|
== Further Reading
|
|
|
|
|
2016-09-11 06:33:36 -04:00
|
|
|
See https://bugs.ruby-lang.org/projects/ruby-trunk/wiki/RefinementsSpec for the
|
2013-01-23 19:40:49 -05:00
|
|
|
current specification for implementing refinements. The specification also
|
|
|
|
contains more details.
|