【发布时间】:2023-03-26 04:16:01
【问题描述】:
我有一个标头方法,它可以将标准标头写入我抛出的所有文本文件。代码如下:
def header(file, description)
File.open(file, 'w') do |out|
out.puts ".------------------------------------------------------------------------."
out.puts "| File generated by: " + Etc.getlogin() + " " * (52-Etc.getlogin().length) + "|"
out.puts "| File generated at: " + Time.now().to_s() + " |"
out.puts ".------------------------------------------------------------------------."
out.puts "| DESCRIPTION: |"
out.puts "| " + description.to_s + " " * (70-description.length) + "|"
out.puts "| |"
out.puts "'------------------------------------------------------------------------'"
out.puts "=====FINDINGS====="
end
end
所以当我运行以下调用语句时:
header('01httpserver.txt', "This file details all configuration files with where http servers are concerned.")
我收到以下错误:
cis.rb:63:in `*': negative argument (ArgumentError)
from cis.rb:63:in `block in header'
from cis.rb:57:in `open'
from cis.rb:57:in `header'
from cis.rb:70:in `<main>'
第 63 行是这一行:
out.puts "| " + description.to_s + " " * (70-description.length) + "|"
我做错了什么?
【问题讨论】:
标签: ruby string string-length