红宝石输出问题

时间:2010-09-21 21:02:38

标签: ruby

我有以下代码:

File.open("/log/#{hostname}-#{@tdate}-errors.txt",'w') do |o|
  run=tn.cmd('String'=>'sh int', 'Match'=>/#/) { |c| puts c}
  run.each_line do |re|
    title = re.match /([\S]+)Ethernet\S+/
    rep = re.match /\d+ input errors/
    #o.puts run
    o.puts title
    o.puts rep 
   end
 end
 tn.close

它写入文件就好了,但在title和rep之间添加了一堆空格。我如何摆脱空间?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用rstrip删除尾随空格http://ruby-doc.org/core/classes/String.html#M000822

答案 1 :(得分:0)

我认为你真的想做这样的事情:

File.open("/log/#{hostname}-#{@tdate}-errors.txt",'w') do |o|
  run = tn.cmd('String'=>'sh int', 'Match'=>/#/) { |c| puts c}
  title = nil
  error_count = nil
  run.each_line do |line|
    title = $1 if line =~/(\S+Ethernet\S+)/
    error_count = Integer($1) if line =~ /(\d+) input errors/
  end
  title ||= 'no title'
  error_count ||= 0
  o.puts title
  o.puts error_count
end
tn.close
相关问题