2011-06-23 75 views
0

我想設置一個布爾變量來切換Net::Telnet模塊在日誌路徑,即:紅寶石網/遠程登錄使用布爾切換登錄

telnetdebug = false 
telnetlog = false 
telnetlogfile = '/var/log/mcacheMonitor.telnet.log' 

xmr = Net::Telnet.new("Host" => host, 
         "Timeout" => 10, 
         "Prompt" => /[#]\z/n, 
         'Waittime' => 0, 
         'Dump_log' => telnetdebug ? "mcmsDebug.log" : nil, 
         'Output_log' => telnetlog ? telnetlogfile : nil) 

但是這個代碼產生以下錯誤:

C:/Ruby192/lib/ruby/1.9.1/net/telnet.rb:300:in `initialize': can't convert nil into String (TypeError) 
     from C:/Ruby192/lib/ruby/1.9.1/net/telnet.rb:300:in `open' 
     from C:/Ruby192/lib/ruby/1.9.1/net/telnet.rb:300:in `initialize' 
     from mcw.rb:26:in `new' 
     from mcw.rb:26:in `<main>' 

回答

3

telnet文檔是cheating。它表示默認值爲零,但實際上不是。如果它有一個鍵'Dump_log',則檢查參數的散列。如果是,則該值用作文件名。所以這應該工作:

telnet_arguments={"Host" => host, 
        "Timeout" => 10, 
        "Prompt" => /[#]\z/n, 
        'Waittime' => 0} 
telnet_arguments['Dump_log'] = "mcmsDebug.log" if telnetdebug 
telnet_arguments['Output_log'] = telnetlogfile if telnetlog 

xmr = Net::Telnet.new(telnet_arguments) 
+3

一個很好的例子「使用源盧克!」。 –