Const_defined?

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

"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