Skip to content

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

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
See also #size? #sticky? #size #symlink? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0