0

I have a chunk of code that has commands. I want to keep more commands in a separate file and pull them into the main. Thus my main file can be updated without losing any custom commands. How would I go about that?

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

1 回答 1

2

The answe above will not work in this case because there is no listen_for methods defined in the custom.rb file

wrap whatever you have in custom.rb in a module, like

module Foo
  # commands
end

require your file custom.rb on the top of your script and include it in your class:

require_relative './custom.rb'

class myClass()
  include Foo
  # code here
end

This is the new try

Remove the module wrapper in the listen_for commands, and instead simply list them in the custom.rb as you would do inside your main class definition. And in your main class, Read and eval it, like this:

class myClass()
  eval(File.read('./custom.rb'))
  # code here
end
于 2013-05-08T20:47:23.420 回答