2012-10-17 96 views
1

我記得之前出現過這個問題,但找不到答案。需求後的Ruby默認類操作

我需要一個文件是這樣的:

#lib/tm/agent/server.rb 
require 'tm/agent/server' 

,並在不顯式調用監聽類,它的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 

如何避免初始化上要求類?

+0

也許這可能有助於http://stackoverflow.com/questions/3745252/require-file-without-executing-code – halfelf

回答

2

這是由於Goliath服務器中的鉤子,當你直接運行你的腳本時會自動啓動服務器 - 這不是Ruby的一個正常特性。

要避免它,請不要撥打require 'goliath',而應使用require 'goliath/api'來代替。

+0

我想我只需要注意一些庫的行爲就是這樣。 – Istvan