There are three implicit contexts in Ruby:
self
(the context for receiverless message sends, and instance variables)
- the default definee (the context for
def
method definition expressions without an explicit target, i.e. def bar
instead of def foo.bar
)
- the default constant definition point
Unfortunately, while the article I linked to above lists all three of them, it only discusses the first two and defers the default constant definition point to a later article, which was never written.
Anyway, it is important to keep those three contexts in mind, and be aware of when they change and when they don't.
In particular, a block only changes the lexical context and nothing else. A block does not change self
, it does not change the default definee and it does not change the default constant definition point.
However, there are some methods whose explicit purpose it is to change one or more of those three contexts.
The *_eval
family of methods changes self
(context #1) and the default definee (context #2), but it does not change the default constant definition point (context #3). In particular, all *_eval
(*_exec
) methods set self
to the receiver. The instance_*
versions set the default definee to the receiver's singleton class, the module_*
and class_*
versions set the default definee to the receiver.
However, the default constant definition point is not changed, so constant definition (and lookup) works just as before: definitions go to the nearest lexically enclosing module definition, lookup starts at the nearest lexically enclosing module definition and proceeds lexically outwards and dynamically upwards-by-inheritance.
As far as I could find, the only construct that changes the default constant definition point is a module
/class
definition.