const_defined?

S

Shak

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
 
S

Shak

Shak said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top