Is it possible to get rid of the eval statement below? The code below filters out all classes which are derived from type BaseClass. Afterwards those classes are instantiated and method 'hello' is called.
module MySpace
class BaseClass
def hello; print "\nhello world"; end
end
class A<BaseClass
def hello; super; print ", class A was here"; end
end
class B<BaseClass
def hello; super; print ", I'm just a noisy class"; end
end
MySpace.constants.each do | e |
c=eval(e)
if c < BaseClass
c.new.hello
end
end
end
So after execution the output is:
hello world, I'm just a noisy class
hello world, class A was here
I think unnecessary use of eval is evil. And I'm not sure if the use of eval is mandatory here. Is there is a smarter way in invoking all classes from type "BaseClass" dynamically?