mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* doc/syntax/refinements.rdoc: update documentation to reflect
recent changes. [ci skip] [ruby-core:71466] [Misc #11681] Patch by James Adam git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52629 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9876377040
commit
46425732cf
2 changed files with 40 additions and 26 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Wed Nov 18 10:05:25 2015 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* doc/syntax/refinements.rdoc: update documentation to reflect
|
||||||
|
recent changes.
|
||||||
|
[ci skip] [ruby-core:71466] [Misc #11681] Patch by James Adam
|
||||||
|
|
||||||
Wed Nov 18 09:50:21 2015 Naotoshi Seo <sonots@gmail.com>
|
Wed Nov 18 09:50:21 2015 Naotoshi Seo <sonots@gmail.com>
|
||||||
|
|
||||||
* test/logger/test_logdevice.rb: Fix tests of logger to make it work on
|
* test/logger/test_logdevice.rb: Fix tests of logger to make it work on
|
||||||
|
|
|
@ -43,15 +43,21 @@ Activate the refinement with #using:
|
||||||
|
|
||||||
== Scope
|
== Scope
|
||||||
|
|
||||||
You may only activate refinements at top-level, not inside any class, module
|
You may activate refinements at top-level, and inside classes and modules.
|
||||||
or method scope. You may activate refinements in a string passed to
|
You may not activate refinements in method scope. Refinements are activated
|
||||||
Kernel#eval that is evaluated at top-level. Refinements are active until the
|
until the end of the current class or module definition, or until the end of
|
||||||
end of the file or the end of the eval string, respectively.
|
the current file if used at the top-level.
|
||||||
|
|
||||||
Refinements are lexical in scope. When control is transferred outside the
|
You may activate refinements in a string passed to Kernel#eval. Refinements
|
||||||
scope the refinement is deactivated. This means that if you require or load a
|
are active the end of the eval string.
|
||||||
file or call a method that is defined outside the current scope the refinement
|
|
||||||
will be deactivated:
|
Refinements are lexical in scope. Refinements are only active within a scope
|
||||||
|
after the call to using. Any code before the using statement will not have the
|
||||||
|
refinement activated.
|
||||||
|
|
||||||
|
When control is transferred outside the scope the refinement is deactivated.
|
||||||
|
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:
|
||||||
|
|
||||||
class C
|
class C
|
||||||
end
|
end
|
||||||
|
@ -136,6 +142,26 @@ In a file:
|
||||||
end
|
end
|
||||||
# activated here
|
# activated here
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Note that the refinements in M are not activated automatically if the class
|
||||||
|
Foo is reopened later.
|
||||||
|
|
||||||
In eval:
|
In eval:
|
||||||
|
|
||||||
# not activated here
|
# not activated here
|
||||||
|
@ -182,24 +208,6 @@ called:
|
||||||
|
|
||||||
p [{1=>2}, {3=>4}].to_json # prints "[{\"1\":2},{\"3\":4}]"
|
p [{1=>2}, {3=>4}].to_json # prints "[{\"1\":2},{\"3\":4}]"
|
||||||
|
|
||||||
You may also activate refinements in a class or module definition, in which
|
|
||||||
case the refinements are activated from the point where using is called to
|
|
||||||
the end of the class or module definition:
|
|
||||||
|
|
||||||
# not activated here
|
|
||||||
class Foo
|
|
||||||
# not activated here
|
|
||||||
using M
|
|
||||||
# activated here
|
|
||||||
def foo
|
|
||||||
# activated here
|
|
||||||
end
|
|
||||||
# activated here
|
|
||||||
end
|
|
||||||
# not activated here
|
|
||||||
|
|
||||||
Note that the refinements in M are not activated automatically even if the class
|
|
||||||
Foo is reopened later.
|
|
||||||
|
|
||||||
== Method Lookup
|
== Method Lookup
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue