Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

TCPSocket.gethostbyname

TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]

Use Addrinfo.getaddrinfo instead. This method is deprecated for the following reasons:

  • The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.

  • gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)

  • This method uses gethostbyname() function already removed from POSIX.

This method lookups host information by hostname.

Example
TCPSocket.gethostbyname("localhost")
#=> ["localhost", ["hal"], 2, "127.0.0.1"]
See also .new also in 4.0 · 3.4 · 3.3 · 3.1 · 3.0