Skip to content


Let Module#const_defined? check Object in 1.9 if search_parents is true #1624

merged 2 commits into from

2 participants


In Ruby 1.9 Module#const_defined? checks the entire ancestors chain, and special-cases modules if search_parents is true. Reported in #1622.

@ileitch ileitch merged commit f5f8efe into rubinius:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 15 additions and 1 deletion.
  1. +5 −1 kernel/common/module19.rb
  2. +10 −0 spec/ruby/core/module/const_defined_spec.rb
6 kernel/common/module19.rb
@@ -47,10 +47,14 @@ def const_defined?(name, search_parents=true)
if search_parents
current = self.direct_superclass
- while current and current != Object
+ while current
return true if current.constant_table.has_key? name
current = current.direct_superclass
+ if instance_of?(Module)
+ return true if Object.constant_table.has_key? name
+ end
return false
10 spec/ruby/core/module/const_defined_spec.rb
@@ -20,6 +20,16 @@
ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST10).should be_true
+ it "returns true if the constant is defined in Object and the receiver is a module" do
+ # CS_CONST1 is defined in Object
+ ConstantSpecs::ModuleA.const_defined?(:CS_CONST1).should be_true
+ end
+ it "returns true if the constant is defined in Object and the receiver is a class that has Object among its ancestors" do
+ # CS_CONST1 is defined in Object
+ ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST1).should be_true
+ end
it "returns false if the constant is defined in the receiver's superclass and the inherit flag is false" do
ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, false).should be_false
Something went wrong with that request. Please try again.