如何将配置块拆分为多个文件?

时间:2011-10-16 09:05:37

标签: ruby

鉴于以下示例,我如何将Botsy配置块拆分为多个文件?

这是一个Campfire机器人,它变得越来越复杂;我想更好地组织代码。例如,我希望将其配置拆分为文件或模块,例如“聊天”,“命令”等,同时仅实例化一个Bot。

Botsy::Bot.new(campfire["subdomain"], campfire["token"], campfire["room"]) do

  hear(/.*(going down|happening|news|up to)\?/i) do |data|
    say "I caught my first tube today."
  end

  #...lots more configuration

end

2 个答案:

答案 0 :(得分:0)

我认为如果将Botsy :: Bot的新实例分配给某个类的常量或实例变量,则可以使用它并稍后定义更多配置。

MYBOT = Botsy::Bot.new(campfire["subdomain"], campfire["token"], campfire["room"]) do

  hear(/.*(going down|happening|news|up to)\?/i) do |data|
    say "I caught my first tube today."
  end

  # ...
end

####
# Different file which is evaluated before the bot is set listening

MYBOT.hear(/hello world/) do |data|
  say "Hello, humans!"
end

查看来源。 https://github.com/seejohnrun/botsy/blob/master/lib/botsy.rb

答案 1 :(得分:0)

解决了:

Botsy::Bot.new(campfire["subdomain"], campfire["token"], campfire["room"]) do

  instance_eval File.read('./banter.rb')
  instance_eval File.read('./commands.rb')

end

其中banter.rb包含类似的内容:

hear(/.*(going down|happening|news|up to)\?/i) do |data|
  say "I caught my first tube today."
end