# Const\_defined?

**URL:** <https://rubytalk.org/t/const-defined/56488>\
**Category:** ruby-talk\
**Created:** [4 December 2009 16:15 UTC](https://rubytalk.org/t/const-defined/56488 "2009-12-04T16:15:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shak1](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Shak1](https://rubytalk.org/u/Shak1)\
**Post date:** [4 December 2009 16:15 UTC](https://rubytalk.org/t/const-defined/56488/1 "2009-12-04T16:15:09Z")

</div>

Hi all,

Why would const-defined? as used below throw a NameError? Surely the whole  
point of the method is to tell you what \*isn't\* defined? Or are we supposed  
to look for an exception instead of false? If so, what's the best way of  
knowing whether a module contains a certain class or not?

> **···**
>
> ======  
> module Space  
> &nbsp;&nbsp;class Time  
> &nbsp;&nbsp;&nbsp;&nbsp;def hello  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Hello world!'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> s = Space::Time.new  
> s.hello #Hello world!
> 
> clazz = Space.const\_get('Time') #get class with name  
> obj = clazz.new #instatiate object  
> obj.hello #Hello world!
> 
> # puts Space.const\_defined?('Time') # true puts Space.const\_defined?('ime') #throws nameerror
> 
> Thanks!
> 
> Shak

---

<div class="post-metadata">

**Author:** ![Shak1](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Shak1](https://rubytalk.org/u/Shak1)\
**Post date:** [4 December 2009 16:30 UTC](https://rubytalk.org/t/const-defined/56488/2 "2009-12-04T16:30:21Z")

</div>

"Shak" \<sshaikh@hotmail.com\> wrote in message news:uyaSm.3016$Cb4.1071@newsfe03.ams2...

> Hi all,
> 
> Why would const-defined? as used below throw a NameError? Surely the whole  
> point of the method is to tell you what \*isn't\* defined? Or are we supposed  
> to look for an exception instead of false? If so, what's the best way of  
> knowing whether a module contains a certain class or not?

It seems to croak because the argument passed isn't a constant name, that is it doesn't start with a capital letter.

puts Space.const\_defined?('Lop')

returns false as required.

Shak
