从 Ruby 1.93 收到此错误,代码较旧(为 Ruby 1.6 编写并运行)
makeCores.rb:2136:in block (2 levels) in same_contents': undefined method
>' for nil:NilClass (NoMethodError)
如果 f1.lstat.blksize > 0,第 2136 行就是这个
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:inblock (2 levels) in same_contents': undefined method
File' for # (NoMethodError)
如何使用 Ruby 1.93 更新它?
目前在 Windows 7 上,可能是为 Win XP 开发的。有没有比 blksize 更好的替代方法或更好的使用方法?