是否有可能摆脱下面的评估声明?下面的代码过滤掉从BaseClass类型派生的所有类。之后,这些类将被实例化,并调用方法'hello'。如何在不使用eval的情况下动态调用类?
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
所以执行后的输出结果是:
世界你好,我只是一个吵吵闹闹的班级
的Hello World,A类在这里
我认为没有必要使用的eval是邪恶的。而且我不确定是否使用eval这里是强制性的。动态调用“BaseClass”类型的所有类是否有更明智的方法?
FWIW,链接被破坏。 – 2012-01-04 04:19:50