0

从 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 更好的替代方法或更好的使用方法?

4

1 回答 1

1

我建议你改变:

if f1.lstat.blksize > 0

if f1.lstat.blksize && f1.lstat.blksize > 0

这应该以向后兼容的方式保持相同的逻辑:未提供块大小零值意味着使用默认的硬编码值)。nil1.6版本后引入for“不支持”的返回值

此处为 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

于 2013-09-14T18:17:58.803 回答