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” [email protected] wrote in message
news:[email protected]

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