# Const\_defined? raises NameError?

**URL:** <https://rubytalk.org/t/const-defined-raises-nameerror/12944>\
**Category:** ruby-talk\
**Created:** [7 August 2004 13:56 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944 "2004-08-07T13:56:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [7 August 2004 13:56 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944/1 "2004-08-07T13:56:48Z")

</div>

Hi,

Given a const how can I check whether the const exists  
without rescuing an exception thrown - if it isn't?  
(using const\_set is not elegant since You have to supply a special value)

if const\_defined?('SomeConst')  
&nbsp;&nbsp;...  
end

My next qusetion is:  
Assuming there is a nested const:

module SomeModule  
&nbsp;&nbsp;Const="somevalue"  
end

the only way to access it by reflection is:

s='SomeModule::Const'  
mod,con=s.split(/::/).map{|e|e.strip}  
Object.const\_get(mod).const\_get(con)

wouldn't it be easier if the following worked avoiding any eval calls:

object.const\_get('SomeModule::Const')

TIA

Marcin Mielzynski

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [7 August 2004 14:12 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944/2 "2004-08-07T14:12:44Z")

</div>

Hi --

> Hi,
> 
> Given a const how can I check whether the const exists  
> without rescuing an exception thrown - if it isn't?  
> (using const\_set is not elegant since You have to supply a special value)
> 
> if const\_defined?('SomeConst')  
> &nbsp;&nbsp;...  
> end

if defined?(SomeConst)

> My next qusetion is:  
> Assuming there is a nested const:
> 
> module SomeModule  
> &nbsp;&nbsp;Const="somevalue"  
> end
> 
> the only way to access it by reflection is:
> 
> s='SomeModule::Const'  
> mod,con=s.split(/::/).map{|e|e.strip}  
> Object.const\_get(mod).const\_get(con)
> 
> wouldn't it be easier if the following worked avoiding any eval calls:
> 
> object.const\_get('SomeModule::Const')

I've seen code floating around that does that. It's not too hard to  
implement (definitely no eval needed):

&nbsp;&nbsp;def const\_get\_deep(str)  
&nbsp;&nbsp;&nbsp;&nbsp;str.split("::").inject(Object) {|x,y| x = x.const\_get(y)}  
&nbsp;&nbsp;end

&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;&nbsp;class B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C = 1  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;p const\_get\_deep("A:🅱:C") # 1

I guess it might be useful to have this in the core. I don't know  
whether it's been considered by Matz in the past.

David

> **···**
>
> On Sat, 7 Aug 2004, [ISO-8859-2] Marcin Miel¿yñski wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [7 August 2004 14:36 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944/3 "2004-08-07T14:36:23Z")

</div>

Hi,

\>  
\> if defined?(SomeConst)  
\>

Thanks for Your answer, but I think the first question is still open  
since the Const is given as a String and the const\_defined?  
raises an error.  
Of course a can catch it but on the other hand I don't know how exception mechanisms in Ruby affect performance.

Marcin Milezynski

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [7 August 2004 14:41 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944/4 "2004-08-07T14:41:19Z")

</div>

> Thanks for Your answer, but I think the first question is still open  
> since the Const is given as a String and the const\_defined?  
> raises an error.

const\_defined? is a method of Module, not Kernel

svg% ruby -e 'p Object.const\_defined?("Array")'  
true  
svg%

svg% ruby -e 'p Object.const\_defined?("Arr")'  
false  
svg%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [7 August 2004 14:51 UTC](https://rubytalk.org/t/const-defined-raises-nameerror/12944/5 "2004-08-07T14:51:21Z")

</div>

Dnia 2004-08-07 16:41, U¿ytkownik ts napisa³:

> const\_defined? is a method of Module, not Kernel

Ups, that's wright 🙂

Marcin Mielzynski
