Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.ftype
(::path file_name) -> ::String
File.ftype(path) → string
file.c
View on GitHub ↗
static VALUE
rb_file_s_ftype(VALUE klass, VALUE fname)
{
struct stat st;
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (lstat_without_gvl(StringValueCStr(fname), &st) == -1) {
rb_sys_fail_path(fname);
}
return rb_file_ftype(st.st_mode);
}
Returns the string type of the object at path, one of:
-
'file'. -
'directory'. -
'characterSpecial'. -
'blockSpecial'. -
'fifo'. -
'link'. -
'socket'.
Examples:
Example
File.ftype('/etc/passwd') # => "file" File.ftype('/etc') # => "directory" File.ftype("/dev/null") # => "characterSpecial" File.ftype("/dev/loop0") # => "blockSpecial" File.mkfifo('/tmp/pipe', 0o666) File.ftype('/tmp/pipe') # => "fifo" File.symlink('/etc/passwd', '/tmp/link') File.ftype('/tmp/link') # => "link" require 'socket' UNIXServer.new('/tmp/socket') File.ftype('/tmp/socket') # => "socket" File.delete('/tmp/link', '/tmp/pipe', '/tmp/socket') # Clean up.
Returns 'unknown' if the type cannot be determined.