`File :: exists?`和`File :: exists?`之间的区别

时间:2021-08-17 01:33:34

On ruby-doc, the documentation entries for File::exist? and File::exists? are duplicated with different semantics: one entry says returns true if file_name is a directory; the other says returns true if file_name is a file.

在ruby-doc上,File ::的文档条目存在吗?和File ::存在?与不同的语义重复:如果file_name是一个目录,则一个条目返回true;如果file_name是文件,则另一个说返回true。

I don't think either entry is correct. Both methods seem to be implemented in file.c using rb_file_exist_p, which, seems to try to call fstat() if the value passed is an IO, or stat() if it's a string. Both fstat() and stat() return 0 on success and -1 on error, and this is passed back to rb_file_exist_p, and turned into a boolean result. It seems to me that

我不认为任何一个条目是正确的。这两种方法似乎都是使用rb_file_exist_p在file.c中实现的,如果传递的值是IO,则似乎尝试调用fstat(),如果是字符串,则尝试调用stat()。 fstat()和stat()在成功时返回0,在出错时返回-1,并将其传递回rb_file_exist_p,并转换为布尔结果。在我看来,这

  1. there are two methods for making code read more easily; there are no semantic differences
  2. 有两种方法可以更容易地读取代码;没有语义差异
  3. neither really relates to a file existing, but to whether a file-like item exists, e.g. a file, a dir, a socket, a fifo etc.
  4. 既不存在与存在的文件有关,也与存在文件的项目是否存在有关,例如文件,dir,socket,fifo等
  5. perhaps the document could say that the methods tell the caller whether or not a thing that has file-like semantics is there, but more specific tests will tell what it actually is: e.g. directory?, file?, socket? etc.
  6. 也许文档可以说这些方法告诉调用者是否存在具有类文件语义的东西,但是更具体的测试将告诉它实际上是什么:例如目录?,文件?,socket?等等

Is my understanding of the (lack of) difference in the methods correct, and is it worth suggesting a change to the document ?

我对方法中(缺乏)差异的理解是否正确,是否值得建议对文档进行更改?

4 个解决方案

#1


20  

Note that the answer to this question depends on the Ruby version. See the other answers for newer versions of Ruby.

请注意,此问题的答案取决于Ruby版本。查看更新版本Ruby的其他答案。


If we look at the C source, we see this:

如果我们查看C源代码,我们会看到:

rb_cFile = rb_define_class("File", rb_cIO);
/* ... */
define_filetest_function("exist?", rb_file_exist_p, 1);
define_filetest_function("exists?", rb_file_exist_p, 1);

So File.exist? and File.exists? are exactly the same thing and the corresponding documentation is:

File.exist?和File.exists?是完全相同的东西,相应的文档是:

Return <code>true</code> if the named file exists.

The rb_file_exist_p C function is just a very thin wrapper around rb_stat, that's a wrapper for the STAT macro, and STAT is just a portability wrapper for the stat system call. So, the documentation above is correct: File#exist? returns true if the file exists.

rb_file_exist_p C函数只是rb_stat的一个非常薄的包装器,它是STAT宏的包装器,而STAT只是stat系统调用的可移植包装器。所以,上面的文档是正确的:文件#存在?如果文件存在,则返回true。

If we check file.c for the documentation snippet that talks about directories, we find this:

如果我们检查file.c以获取谈论目录的文档片段,我们会发现:

/*
 * Document-method: exist?
 *
 * call-seq:
 *   Dir.exist?(file_name)   ->  true or false
 *   Dir.exists?(file_name)   ->  true or false
 *
 * Returns <code>true</code> if the named file is a directory,
 * <code>false</code> otherwise.
 *
 */

So it looks like the documentation generator is getting confused because Dir.exist? and File.exist? are documented in file.c even though Dir is defined in dir.c.

所以看起来文档生成器变得混乱,因为Dir.exist?和File.exist?即使在dir.c中定义了Dir,也会记录在file.c中。

The underlying problem seems to be that the source code arrangement doesn't match what the documentation generator expects and the result is confused and incorrect documentation. I'm not sure how this should be fixed though.

潜在的问题似乎是源代码安排与文档生成器所期望的不匹配,结果是混淆和不正确的文档。我不确定应该如何解决这个问题。

#2


5  

File.exist? and File.exists? are NOT exactly the same thing anymore. See https://github.com/ruby/ruby/blob/ruby_2_3/file.c#L5920

File.exist?和File.exists?不再完全相同了。请参阅https://github.com/ruby/ruby/blob/ruby_2_3/file.c#L5920

define_filetest_function("exist?", rb_file_exist_p, 1);
define_filetest_function("exists?", rb_file_exists_p, 1);

rb_file_exists_p contains this line:

rb_file_exists_p包含以下行:

rb_warning("%sexists? is a deprecated name, use %sexist? instead", s, s);

So you should stick with File.exist?.

所以你应该坚持使用File.exist?。

#3


4  

Since ruby 2.2.0 File.exists? is deprecated use instead File.exist?

由于ruby 2.2.0 File.exists?不推荐使用而不是File.exist?

http://ruby-doc.org/core-2.2.0/File.html#exist-3F-method

http://ruby-doc.org/core-2.2.0/File.html#exist-3F-method

#4


2  

git pull made it go away - this was fixed here - not sure why generated doco on ruby-doc and apidock still wrong

git pull让它消失了 - 这是在这里修复的 - 不知道为什么在ruby-doc和apidock上生成doco仍然错误

#1


20  

Note that the answer to this question depends on the Ruby version. See the other answers for newer versions of Ruby.

请注意,此问题的答案取决于Ruby版本。查看更新版本Ruby的其他答案。


If we look at the C source, we see this:

如果我们查看C源代码,我们会看到:

rb_cFile = rb_define_class("File", rb_cIO);
/* ... */
define_filetest_function("exist?", rb_file_exist_p, 1);
define_filetest_function("exists?", rb_file_exist_p, 1);

So File.exist? and File.exists? are exactly the same thing and the corresponding documentation is:

File.exist?和File.exists?是完全相同的东西,相应的文档是:

Return <code>true</code> if the named file exists.

The rb_file_exist_p C function is just a very thin wrapper around rb_stat, that's a wrapper for the STAT macro, and STAT is just a portability wrapper for the stat system call. So, the documentation above is correct: File#exist? returns true if the file exists.

rb_file_exist_p C函数只是rb_stat的一个非常薄的包装器,它是STAT宏的包装器,而STAT只是stat系统调用的可移植包装器。所以,上面的文档是正确的:文件#存在?如果文件存在,则返回true。

If we check file.c for the documentation snippet that talks about directories, we find this:

如果我们检查file.c以获取谈论目录的文档片段,我们会发现:

/*
 * Document-method: exist?
 *
 * call-seq:
 *   Dir.exist?(file_name)   ->  true or false
 *   Dir.exists?(file_name)   ->  true or false
 *
 * Returns <code>true</code> if the named file is a directory,
 * <code>false</code> otherwise.
 *
 */

So it looks like the documentation generator is getting confused because Dir.exist? and File.exist? are documented in file.c even though Dir is defined in dir.c.

所以看起来文档生成器变得混乱,因为Dir.exist?和File.exist?即使在dir.c中定义了Dir,也会记录在file.c中。

The underlying problem seems to be that the source code arrangement doesn't match what the documentation generator expects and the result is confused and incorrect documentation. I'm not sure how this should be fixed though.

潜在的问题似乎是源代码安排与文档生成器所期望的不匹配,结果是混淆和不正确的文档。我不确定应该如何解决这个问题。

#2


5  

File.exist? and File.exists? are NOT exactly the same thing anymore. See https://github.com/ruby/ruby/blob/ruby_2_3/file.c#L5920

File.exist?和File.exists?不再完全相同了。请参阅https://github.com/ruby/ruby/blob/ruby_2_3/file.c#L5920

define_filetest_function("exist?", rb_file_exist_p, 1);
define_filetest_function("exists?", rb_file_exists_p, 1);

rb_file_exists_p contains this line:

rb_file_exists_p包含以下行:

rb_warning("%sexists? is a deprecated name, use %sexist? instead", s, s);

So you should stick with File.exist?.

所以你应该坚持使用File.exist?。

#3


4  

Since ruby 2.2.0 File.exists? is deprecated use instead File.exist?

由于ruby 2.2.0 File.exists?不推荐使用而不是File.exist?

http://ruby-doc.org/core-2.2.0/File.html#exist-3F-method

http://ruby-doc.org/core-2.2.0/File.html#exist-3F-method

#4


2  

git pull made it go away - this was fixed here - not sure why generated doco on ruby-doc and apidock still wrong

git pull让它消失了 - 这是在这里修复的 - 不知道为什么在ruby-doc和apidock上生成doco仍然错误