Ruby 1.93 收到此错误,代码较旧(为 Ruby 1.6 编写并在 Ruby 1.6 上运行)
makeCores.rb:2136:in 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:in block (2 levels) in same_contents': undefined method
File' for # (NoMethodError)
如何在 Ruby 1.93 中更新它?
目前在Windows 7上,可能是为Win XP开发的。有没有比 blksize 更好的替代方法?
我建议你改变:
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