我在桌面上编写了以下代码,并且运行良好。我在笔记本电脑上下载了它,下载了 ruby (v1.9.3),并尝试运行它,但出现以下错误。我很确定这与第一次使用 Ruby 有关,但当我第一次运行 Ruby 时,我的桌面上从未遇到过这个问题。
C:/Users/Downloads/vscript.rb:18:in 'initialize': string contains null byte (ArgumentError)
from C:/Users/Downloads/vscript.rb:18:in 'open'
from C:/Users/Downloads/vscript.rb:18:in 'main'
第 18 行是 File.open
行:
File.open("filename", "r") do |f|
# Do while there are characters in the text file
f.each do |line|
# Checks to see if any parts in file match the regex and inform the user
if x = line.match(/\d\.\d\.\d{4}\.\d/)
puts "#{x} was found in the file."
end
end
end
请您参考如下方法:
想通了。当我最初编写代码时,文件名用/分隔文件夹。当我将文件下载到笔记本电脑上时,我从使用\'s 的地址栏复制了它的新目录。改变了它,现在工作正常。