从Ruby 1.93收到此错误,代码较旧(为Ruby 1.6编写并运行)
makeCores.rb:2136:block (2 levels) in same_contents': undefined method
>' for nil:NilClass(NoMethodError)
第2136行是这个 如果f1.lstat.blksize> 0
class File
def File.same_contents(file1, file2)
return false if !File.exists?(file1) # The files are different if file1 does not exist
return false if !File.exists?(file2) # The files are different if file2 does not exist
return true if File.expand_path(file1) == File.expand_path(file2) # The files are the same if they are the same file
# Otherwise, compare the files contents, one block at a time (First check if both files are readable)
if !File.readable?(file1) || !File.readable?(file2)
puts "\n>> Warning : Unable to read either xco or input parameters file"
return false
end
open(file1) do |f1|
open(file2) do |f2|
if f1.lstat.blksize > 0
blocksize = f1.lstat.blksize # Returns the native file system's block size.
else
blocksize = 4096 # Set to a default value for platforms that don't support this information (like Windows)
end
same = true
while same && !f1.eof? && !f2.eof?
same = f1.read(blocksize) == f2.read(blocksize)
end
return same
end
end
end
end
我可以看到lstat和blksize方法仍然存在。
尝试将lstat更改为File.lstat,错误消息更改为:
makeCores.rb:2137:在block (2 levels) in same_contents': undefined method
文件'中#(NoMethodError)
如何使用Ruby 1.93更新它?
目前在Windows 7上,可能是为Win XP开发的。 是否有比blksize更换或更好的方法?
答案 0 :(得分:1)
我建议你改变:
if f1.lstat.blksize > 0
到
if f1.lstat.blksize && f1.lstat.blksize > 0
这应该以向后兼容的方式保持相同的逻辑:没有块提供或零值意味着使用默认的硬编码值)。 nil
的返回值为"不支持"是在1.6版之后推出的
此处的文档为1.6:http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_file__stat.html#File::Stat.blksize
这里是1.9.3:http://www.ruby-doc.org/core-1.9.3/File/Stat.html#method-i-blksize