实例化类并推送到数组Ruby

实例化类并推送到数组Ruby,ruby,class,loading,Ruby,Class,Loading,我试图实例化scenes目录中的所有类,并将它们添加到scenes数组中。到目前为止,代码看起来应该可以工作,但我遇到了一个奇怪的rgexp-ct错误,我无法在网上找到任何答案。这是我的密码 class Game @scenes = [] def initialize Dir[File.dirname(__FILE__)+"/scenes/*.rb"].each do |file| require_relative file

我试图实例化scenes目录中的所有类,并将它们添加到scenes数组中。到目前为止,代码看起来应该可以工作,但我遇到了一个奇怪的rgexp-ct错误,我无法在网上找到任何答案。这是我的密码

class Game
    @scenes = []

    def initialize
        Dir[File.dirname(__FILE__)+"/scenes/*.rb"].each do |file|
            require_relative file
            @scenes << eval("#{file.gsub(".rb", "")}.new()")
        end
    end
end
下面是我得到的关于regexp的语法错误,这只在我尝试运行eval(

/Users/icetimux/projects/death at appledore towers/lib/death at appledore towers/game.rb:8:in'eval':(eval):1:unknown regexp选项-ct(SyntaxError)
from/Users/icetimux/projects/death at appledore towers/lib/death at appledore towers/game.rb:8:in'block in initialize'
from/Users/icetimux/projects/death at appledore towers/lib/death at appledore towers/game.rb:6:in'each'
from/Users/icetimux/projects/death at appledore towers/lib/death at appledore towers/game.rb:6:in'initialize'
从appledore towers的死亡中。rb:3:in'new'
从阿普尔多塔的死亡中。rb:3:in`'

如果您有一个名为的文件,比如说,
ct.rb
,那么
file
将是
/scenes/ct.rb
,您将
eval

/scenes/ct.new()
这是非法的
Regexp
literal,与

(/scenes/ct).new()
老实说,我甚至不知道该代码应该做什么,所以我无法提供修复,但这就是问题所在。您盲目地假设任何文件名都将是有效的
Regexp
标志的组合(例如
x
m
)。

尝试这种方法:

@scenes << file.split("/").last.gsub(".rb", "").camelize.constantize.new

我假设该文件是一个类似于
。/scene/scene99.rb'
的字符串,您需要获取一个scenes对象数组。

/scenes/*
可能是罪魁祸首。
*
是一个无效的正则表达式选项。哦!我该如何查看它。我正在尝试实例化目录中的所有类并将它们附加到一个数组中。请检查t他接受了答案。
(/scenes/ct).new()
@scenes << file.split("/").last.gsub(".rb", "").camelize.constantize.new
@scenes << eval("#{file.split("/").last.gsub(".rb", "").camelize}.new")