Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
FileTest#socket?
(::path | ::io file_name) -> bool
File.socket?(object) → true or false
file.c
View on GitHub ↗
static VALUE
rb_file_socket_p(VALUE obj, VALUE fname)
{
#ifndef S_ISSOCK
# ifdef _S_ISSOCK
# define S_ISSOCK(m) _S_ISSOCK(m)
# else
# ifdef _S_IFSOCK
# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
# else
# ifdef S_IFSOCK
# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
# endif
# endif
# endif
#endif
#ifdef S_ISSOCK
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISSOCK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
Returns whether the given object represents a socket; the object may be a path or an IO object:
Example
require 'socket' sock_path = '/tmp/socket' server = UNIXServer.new(sock_path) File.socket?(sock_path) # => true File.delete(sock_path) # Clean up. file_path = '/etc/passwd' File.file?(file_path) # => true File.socket?(file_path) # => false File.socket?($stdin) # => false File.socket?('nosuch') # => false