if (constant != null) {
return constant;
}
// Look in lexical scope
final RubyClass objectClass = context.getCoreLibrary().getObjectClass();
if (lexicalScope != null) {
if (lexicalScope != context.getRootLexicalScope()) {
// Already looked in the top lexical scope, which is module.
lexicalScope = lexicalScope.getParent();
}
while (lexicalScope != context.getRootLexicalScope()) {
constant = lexicalScope.getLiveModule().getConstants().get(name);
if (constant != null) {
return constant;
}
lexicalScope = lexicalScope.getParent();
}
}
// Look in ancestors
for (RubyModule ancestor : module.parentAncestors()) {
constant = ancestor.getConstants().get(name);
if (constant != null) {
return constant;
}
}
// Look in Object and its included modules
if (module.isOnlyAModule()) {
for (RubyModule ancestor : objectClass.selfAndIncludedModules()) {
constant = ancestor.getConstants().get(name);
if (constant != null) {
return constant;
}