Daniel Schierbeck
6/9/2006 4:15:00 PM
Torsten Robitzki wrote:
> Is a class name treated like a constant in ruyb? Or is my documentation
> not up to date? Or is it just an undocumented feature?
Both classes and modules are constants -- this is valid:
Foo = Class.new{}
Bar = Module.new{}
If you want to make sure the object returned by Module#const_get is a
class, you can just test its class:
class Module
def class_get(name)
cl = const_get(name)
unless cl.kind_of? Class
raise TypeError, "`#{cl.inspect}' must be a Class"
end
return cl
end
end
of course, you could also just accept all objects that respond to #new,
which would be quackier:
class Module
def class_get(name)
cl = const_get(name)
unless cl.respond_to? :new
raise TypeError, "`#{cl.inspect}' must respond to `new'"
end
return cl
end
end
Cheers,
Daniel