我记得之前出现过这个问题,但我找不到答案。
我需要这样一个文件:
#lib/tm/agent/server.rb
require 'tm/agent/server'
并且,如果没有显式调用Listen类,它的initialize
将被执行:
module Tm
module Agent
module Server
require 'goliath'
class Listen < Goliath::API
def initialize
puts "WHAT"
end
def response(env)
[200, {}, "Hello World"]
end
end
end #end Server
end #end Agent
end #end Tm
如何避免在require上初始化类?
答案 0 :(得分:2)
这是因为Goliath服务器中的一个钩子会在您直接运行脚本时自动启动服务器 - 这不是Ruby的常规功能。
为避免这种情况,请不要拨打require 'goliath'
,而是使用require 'goliath/api'
。