End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
IPSocket.getaddress
IPSocket.getaddress(host) => ipaddress
ext/socket/ipsocket.c
View on GitHub ↗
static VALUE
ip_s_getaddress(VALUE obj, VALUE host)
{
union_sockaddr addr;
struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, 0);
socklen_t len = res->ai->ai_addrlen;
/* just take the first one */
memcpy(&addr, res->ai->ai_addr, len);
rb_freeaddrinfo(res);
return rsock_make_ipaddr(&addr.addr, len);
}
Lookups the IP address of host.
Example
require 'socket' IPSocket.getaddress("localhost") #=> "127.0.0.1" IPSocket.getaddress("ip6-localhost") #=> "::1"