1
我有以下代码:动态创建类
module City
class Bus < Base
end
class BusOne < Bus; end
class BusTwo < Bus; end
class BusSixty < Bus; end
....
end
我的目标是动态创建这个类:
class BusOne < Bus; end
class BusTwo < Bus; end
class BusSixty < Bus; end
...
这就是为什么我想:
module City
class Bus < Base
DIVISON = [:one, :two, :sixty]
end
....
Bus::DIVISONS.each do |division|
class "Bus#{division.capitalize}".constantize < Bus; end
end
end
但我得到这个错误:
unexpected '<', expecting &. or :: or '[' or '.' (SyntaxError)
什么我错了吗? 感谢
我认为你的答案值得绿党。我的只是一个变种。 –