# Const\_defined? not quite in sync with const\_get?

**URL:** <https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393>\
**Category:** Ruby\
**Created:** [September 20, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393 "2006-09-20T19:07:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 20, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/1 "2006-09-20T19:07:17Z")

</div>

Assuming the following snippet, I’d expect Foo::const\_defined?(:Bar)  
to be routed via Foo::const\_missing. Instead it resolves to top-level  
Bar. Can someone explain why please?

–code–

class Bar  
end

module Foo  
def self.const\_missing(konst)  
puts “missing konst #{konst}”  
end  
end

Foo::Bar  
#=\> missing konst Bar # Unsurprising behaviour

p Foo::const\_defined?(:Bar)  
false # Unsurprising behaviour

p Foo::const\_get(:Bar)  
#=\> Bar # Surprising behaviour

–/code–

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 20, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/2 "2006-09-20T19:35:22Z")

</div>

> non-prefixed constants follow the normal scoping rules:

Thanks for the clarification. Purely out of interest, do you find  
this behaviour a bit odd (or am I missing obvious reasons as to why it  
is implemented this way)?

Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 20, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/3 "2006-09-20T19:21:20Z")

</div>

On Thu, 21 Sep 2006, Chris R. wrote:

> def self.const\_missing(konst)  
> p Foo::const\_get(:Bar)  
> #=\> Bar # Surprising behaviour
> 
> –/code–
> 
> Cheers,
> 
> Chris

non-prefixed constants follow the normal scoping rules:

harp:~ \> cat a.rb  
class Bar  
end

module Foo  
def self.const\_missing(konst)  
puts “missing konst #{konst}”  
end  
end

Foo::Bar  
#=\> missing konst Bar # Unsurprising behaviour

p Foo::const\_defined?(‘Bar’)  
false # Unsurprising behaviour

p Foo::const\_get(‘Foo::Bar’)

# ^^^^^

# ^^^^^

# ^^^^^

#=\> Bar # Surprising behaviour

harp:~ \> ruby a.rb  
missing konst Bar  
false  
a.rb:16:in `const\_get’: wrong constant name Foo::Bar (NameError)  
from a.rb:16

-a

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 20, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/4 "2006-09-20T22:25:59Z")

</div>

> class C  
> a = const\_get(‘Array’).new  
> end
> 
> behave
> 
> i agree it can be confusing, but the alternative, requiring all constants to  
> be fully scoped, would be an enormous pain in the ass.

Ah, of course, that makes sense. Cheers.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 20, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/5 "2006-09-20T19:59:24Z")

</div>

On Thu, 21 Sep 2006, Chris R. wrote:

> > non-prefixed constants follow the normal scoping rules:
> 
> Thanks for the clarification. Purely out of interest, do you find  
> this behaviour a bit odd (or am I missing obvious reasons as to why it  
> is implemented this way)?
> 
> Chris

so that

class C  
a = Array.new # up scope  
end

and

class C  
a = const\_get(‘Array’).new  
end

behave

i agree it can be confusing, but the alternative, requiring all  
constants to  
be fully scoped, would be an enormous pain in the ass.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 21, 2006, 5:40am UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/6 "2006-09-21T05:40:34Z")

</div>

Date: Thu, 21 Sep 2006 02:06:56 +0900  
Posted: Wed, 20 Sep 2006 18:06:54 +0100  
From: “Chris R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Reply-To: removed\_email\_address@domain.invalid  
Subject: const\_defined? not quite in sync with const\_get ??  
To: removed\_email\_address@domain.invalid (ruby-talk ML)  
Message-Id:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
X-ML-Name: ruby-talk  
X-Mail-Count: 215393

2006/9/21, Chris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> def self.const\_missing(konst)  
> p Foo::const\_get(:Bar)  
> #=\> Bar # Surprising behaviour
> 
> –/code–
> 
> Cheers,
> 
> Chris

–  
e$B$J$+$@$G$9!#e(B

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [September 21, 2006, 5:42am UTC](https://www.ruby-forum.com/t/const-defined-not-quite-in-sync-with-const-get/69393/7 "2006-09-21T05:42:34Z")

</div>

Hi,

Sorry for mispost.

At Thu, 21 Sep 2006 02:06:56 +0900,  
Chris R. wrote in [ruby-talk:215393]:

> Foo::Bar  
> #=\> missing konst Bar # Unsurprising behaviour
> 
> p Foo::const\_defined?(:Bar)  
> false # Unsurprising behaviour
> 
> p Foo::const\_get(:Bar)  
> #=\> Bar # Surprising behaviour

It has changed in 1.9.

p Foo::const\_defined?(:Bar) #=\> true

p Foo::const\_get(:Bar) #=\> Bar

p Foo::const\_defined?(:Bar, false) #=\> false

p Foo::const\_get(:Bar, false) #=\> missing konst Bar
