# Const\_defined?

**URL:** <https://www.ruby-forum.com/t/const-defined/179821>\
**Category:** Ruby\
**Created:** [December 4, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/const-defined/179821 "2009-12-04T17:19:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshaikh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@sshaikh](https://www.ruby-forum.com/u/sshaikh)\
**Post date:** [December 4, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/const-defined/179821/1 "2009-12-04T17:19:33Z")

</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  
class Time  
def hello  
puts ‘Hello world!’  
end  
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:** ![sshaikh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@sshaikh](https://www.ruby-forum.com/u/sshaikh)\
**Post date:** [December 4, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/const-defined/179821/2 "2009-12-04T17:30:40Z")

</div>

“Shak” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:uyaSm.3016$removed\_email\_address@domain.invalid2…

> 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
