Ruby Net / Telnet使用布尔来切换日志记录

时间:2011-06-23 20:13:44

标签: ruby telnet

我正在尝试设置一个布尔变量来切换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)

BUt此代码会产生以下错误:

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>'

1 个答案:

答案 0 :(得分: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 )