我有一大堆有命令的代码。我想将更多命令保存在一个单独的文件中并将它们拉入主文件中。因此,我的主文件可以更新,而不会丢失任何自定义命令。我该怎么做呢?
{
class myClass()
#commands
listen_for /what is your name/i do
say "my name is mud"
end
## Insert Custom.rb here ##
# Everything in the custom rb file is just ike the "listen_for" command above
end
}
答案 0 :(得分:2)
上面的回答在这种情况下不起作用,因为custom.rb文件中没有定义listen_for方法
将custom.rb
中的任何内容包装在模块中,例如
module Foo
# commands
end
要求您的文件custom.rb
位于您的脚本顶部,并将其包含在您的课程中:
require_relative './custom.rb'
class myClass()
include Foo
# code here
end
这是新尝试
删除listen_for命令中的module
包装器,而不是像在主类定义中那样在custom.rb
中列出它们。在你的主要课程中,阅读并评估它,如下所示:
class myClass()
eval(File.read('./custom.rb'))
# code here
end