Method: Socket.gethostbyname

Defined in:
socket.c

.gethostbyname(hostname) ⇒ Array

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.

  • Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.

  • 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 obtains the host information for hostname.

p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]

Returns:

  • (Array)


963
964
965
966
967
968
969
970
# File 'socket.c', line 963

static VALUE
sock_s_gethostbyname(VALUE obj, VALUE host)
{
    rb_warn("Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.");
    struct rb_addrinfo *res =
        rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
    return rsock_make_hostent(host, res, sock_sockaddr);
}