exist?(p1) public

Return true if the named file exists.

Show source
Register or log in to add new notes.
July 31, 2009 - (v1_8_7_72)
1 thank



January 13, 2013
0 thanks

Note sure if doco is correct

(Note this was an issue in Ruby 1.9.2, 1.9.3 has been corrected, not sure why the generated doc is still incorrect)

Both exist? and exists? use the same underlying C function

file.c, line 5444

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

rb_file_exist_p does an rb_stat call, and just checks for no error.

rb_stat returns the result of a call to fstat, if the passed in value is a IO object, or stat (or your platforms equivalent). Both these return 0 on success, -1 on failure.

So both really just check that the underlying “thing” can respond to “stat” correctly. There are many things in a unix-style filesystem that have a “file” structure, not just traditional files. These functions help when you don’t care what type an entry is, just that it exists.

There doesn’t seem to be any difference in the two methods

File.directory? can test if a named file is a dir