43 |
43 |
|
44 |
44 |
== Scope
|
45 |
45 |
|
46 |
|
You may only activate refinements at top-level, not inside any class, module
|
47 |
|
or method scope. You may activate refinements in a string passed to
|
48 |
|
Kernel#eval that is evaluated at top-level. Refinements are active until the
|
49 |
|
end of the file or the end of the eval string, respectively.
|
|
46 |
You may activate refinements at top-level, and inside classes and modules.
|
|
47 |
You may not activate refinements in method scope. Refinements are activated
|
|
48 |
until the end of the current class or module definition, or until the end of
|
|
49 |
the current file if used at the top-level.
|
50 |
50 |
|
51 |
|
Refinements are lexical in scope. When control is transferred outside the
|
52 |
|
scope the refinement is deactivated. This means that if you require or load a
|
53 |
|
file or call a method that is defined outside the current scope the refinement
|
54 |
|
will be deactivated:
|
|
51 |
You may activate refinements in a string passed to Kernel#eval. Refinements
|
|
52 |
are active the end of the eval string.
|
|
53 |
|
|
54 |
Refinements are lexical in scope. Refinements are only active within a scope
|
|
55 |
after the call to using. Any code before the using statement will not have the
|
|
56 |
refinement activated.
|
|
57 |
|
|
58 |
When control is transferred outside the scope the refinement is deactivated.
|
|
59 |
This means that if you require or load a file or call a method that is defined
|
|
60 |
outside the current scope the refinement will be deactivated:
|
55 |
61 |
|
56 |
62 |
class C
|
57 |
63 |
end
|
... | ... | |
136 |
142 |
end
|
137 |
143 |
# activated here
|
138 |
144 |
|
|
145 |
In a class:
|
|
146 |
|
|
147 |
# not activated here
|
|
148 |
class Foo
|
|
149 |
# not activated here
|
|
150 |
def foo
|
|
151 |
# not activated here
|
|
152 |
end
|
|
153 |
using M
|
|
154 |
# activated here
|
|
155 |
def bar
|
|
156 |
# activated here
|
|
157 |
end
|
|
158 |
# activated here
|
|
159 |
end
|
|
160 |
# not activated here
|
|
161 |
|
|
162 |
Note that the refinements in M are not activated automatically if the class
|
|
163 |
Foo is reopened later.
|
|
164 |
|
139 |
165 |
In eval:
|
140 |
166 |
|
141 |
167 |
# not activated here
|
... | ... | |
182 |
208 |
|
183 |
209 |
p [{1=>2}, {3=>4}].to_json # prints "[{\"1\":2},{\"3\":4}]"
|
184 |
210 |
|
185 |
|
You may also activate refinements in a class or module definition, in which
|
186 |
|
case the refinements are activated from the point where using is called to
|
187 |
|
the end of the class or module definition:
|
188 |
|
|
189 |
|
# not activated here
|
190 |
|
class Foo
|
191 |
|
# not activated here
|
192 |
|
using M
|
193 |
|
# activated here
|
194 |
|
def foo
|
195 |
|
# activated here
|
196 |
|
end
|
197 |
|
# activated here
|
198 |
|
end
|
199 |
|
# not activated here
|
200 |
|
|
201 |
|
Note that the refinements in M are not activated automatically even if the class
|
202 |
|
Foo is reopened later.
|
203 |
211 |
|
204 |
212 |
== Method Lookup
|
205 |
213 |
|