It’s not in the scope of the Object class? Then why does this work:
num = 10
Object.module_eval(‘num’)
–>10
According to the method resolution order (MRO), ruby looks for the
attribute in the local scope first, then in the containing scope which
is exposed to the local scope. So in this case, num is the in local
scope for Object#module_eval, i.e., the local scope is self (which is
“main”). If it’s not in the local scope, then in this instance the
exposed containing scope is the Object class. Since ‘num’ is not in
the Object class, if you call Object.module_eval(‘num’) w/o defining
num first, you get the same error. In other words, when you call
Object#module_eval from the top-level, the local scope is the top-
level. But when you call it from inside a function, the local scope is
the function. You can’t access the higher scope of main from within
the lower scope of the function. To do that you need to use a global
variable, which exposes itself to all of the lower scopes of main.
Hmmm…I’m probably not explaining that very well. Basically, anything
you define in the top-level scope is only accessible from the top-
level scope. For nested scopes you either have to pass the data as a
parameter of the function, or use a global variable. Hope that makes
some sense. I’m tired, so forgive me if I’m being incoherent!