包括来自单独Ruby文件的命令

包括来自单独Ruby文件的命令,ruby,siri,Ruby,Siri,我有一段包含命令的代码。我想将更多命令保存在一个单独的文件中,并将它们拉入主文件。因此,可以在不丢失任何自定义命令的情况下更新我的主文件。我该怎么做呢 { 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 "

我有一段包含命令的代码。我想将更多命令保存在一个单独的文件中,并将它们拉入主文件。因此,可以在不丢失任何自定义命令的情况下更新我的主文件。我该怎么做呢

{
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
}

在这种情况下,上述answe将不起作用,因为custom.rb文件中定义的方法没有侦听单元

custom.rb
中的所有内容包装到模块中,如

module Foo
  # commands
end
在脚本顶部要求文件
custom.rb
,并将其包含在类中:

require_relative './custom.rb'

class myClass()
  include Foo
  # code here
end
这是新尝试

删除listen_中的
模块
包装器以获取命令,而只需在
custom.rb
中列出它们,就像在主类定义中那样。在你的主要课堂上,阅读并评估它,如下所示:

class myClass()
  eval(File.read('./custom.rb'))
  # code here
end

部分工作。当我在custom.rb文件中包含更多listen_for命令时,我得到了错误未定义方法“listen_for”您所说的“include more listen_for”命令是什么意思?你能编辑你的问题,包括你的文件是什么样子的以及错误是什么吗?我的课和上面的一模一样。我正在与siri proxy合作。侦听是它接受的命令。这是主siriproxy.rb文件。我要做的是阻止用户编辑此文件。因此,我希望用户将所有“listen_for”命令放在一个单独的文件中,并将其包括在内。我已经把它包括在丰肛建议中了。但现在我遇到了一个未定义的方法“listen_for”,当它被拉进来时。我明白了。编辑了我的答案,看一看。仍然得到main:Object的unfind方法“listen\u for”