Skip to content

Commit a376037

Browse files
committed
Add spec for const_defined class pathing from Object
Class path searching should not include Object's constants.
1 parent 765a1d4 commit a376037

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

core/src/main/java/org/jruby/RubyModule.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,11 +4157,9 @@ private boolean constDefined(Ruby runtime, IRubyObject name, boolean inherit) {
41574157
int patternIndex;
41584158
int index = 0;
41594159
RubyModule mod = this;
4160-
boolean includeObject = false;
41614160

41624161
if (value.startsWith(pattern)) {
41634162
mod = runtime.getObject();
4164-
includeObject = true;
41654163
currentOffset += 2;
41664164
}
41674165

@@ -4206,7 +4204,7 @@ private boolean constDefined(Ruby runtime, IRubyObject name, boolean inherit) {
42064204

42074205
String id = RubySymbol.newConstantSymbol(runtime, fullName, lastSegment).idString();
42084206

4209-
return mod.getConstantSkipAutoload(id, inherit, includeObject) != null;
4207+
return mod.getConstantSkipAutoload(id, inherit, false) != null;
42104208
}
42114209

42124210
// MRI: rb_mod_const_get
@@ -4813,10 +4811,10 @@ public IRubyObject getConstantNoConstMissing(String name, boolean inherit) {
48134811
}
48144812

48154813
public IRubyObject getConstantNoConstMissing(String name, boolean inherit, boolean includeObject) {
4816-
IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, true);
4814+
IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, true, includeObject);
48174815

48184816
if (constant == null && !isClass() && includeObject) {
4819-
constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, true);
4817+
constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, true, includeObject);
48204818
}
48214819

48224820
return constant;
@@ -4833,10 +4831,10 @@ public IRubyObject getConstantNoConstMissingSKipAutoload(String name) {
48334831

48344832
// returns null for autoloads that have failed
48354833
private IRubyObject getConstantSkipAutoload(String name, boolean inherit, boolean includeObject) {
4836-
IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, false);
4834+
IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, false, includeObject);
48374835

48384836
if (constant == null && !isClass() && includeObject) {
4839-
constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, false);
4837+
constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, false, includeObject);
48404838
}
48414839

48424840
return constant;
@@ -4860,8 +4858,9 @@ private static SourceLocation iterateConstantEntryNoConstMissing(String name, Ru
48604858
}
48614859

48624860
private static IRubyObject iterateConstantNoConstMissing(String name,
4863-
RubyModule init, boolean inherit, boolean loadConstant) {
4861+
RubyModule init, boolean inherit, boolean loadConstant, boolean includeObject) {
48644862
for (RubyModule mod = init; mod != null; mod = mod.getSuperClass()) {
4863+
if (!includeObject && mod == init.getRuntime().getObject()) break;
48654864
IRubyObject value =
48664865
loadConstant ?
48674866
mod.getConstantWithAutoload(name, null, true) :

spec/ruby/core/module/const_defined_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
ConstantSpecs.const_defined?("::Object").should == true
150150
ConstantSpecs.const_defined?("ClassA::CS_CONST10").should == true
151151
ConstantSpecs.const_defined?("ClassA::CS_CONST10_").should == false
152+
ConstantSpecs.const_defined?("ClassA::String").should == false
152153
end
153154

154155
it "raises a NameError if the name contains non-alphabetic characters except '_'" do

0 commit comments

Comments
 (0)