From: GOTO Kentaro Date: 2002-09-21T10:09:10+09:00 Subject: Re: Error in Sys::Host on FreeBSD At Fri, 20 Sep 2002 22:14:39 +0900, Berger, Daniel wrote: > That's interesting. It *should* work without the each method. I just > tested it on Solaris using an August build of 1.7.2 and it worked ok. The error was raised on FreeBSD release and current. I scanned host.c and found the function host_free() looks needless unless I misunderstand. And ip_addr() doesn't return any value when it is called as an iterator. By the way, two comments about host.c... You wrote Host.ip_addr.each do |ip| # multi-nic system but one has to use something platform dependent, e.g., ifconfig (or ipconfig on ms-windows) to know *all* addresses of a machine, afaik. getbyhostname() or getaddrinfo() do only reverse lookup for a name of host but *not all* interfaces are associated often to the name given by hostname(). I think that use of Socket is not overkill to get michine's addresses because Socket has getaddrinfo() which resolves protocol-independently. It seems pretty hard to provide getaddrinfo() or an equivalent function for various platforms where the function is missing. So, a lazy person's shows another Sys::Host below :) This is IPv6 ready, require "socket" module Sys module Host include Socket::Constants module_function def hostname Socket::gethostname() end def ip_addr(af = AF_UNSPEC, serv = nil, &block) addrs = Socket.getaddrinfo(Socket::gethostname(), serv, af) addrs.map!{|i| i[3]} addrs.uniq! return addrs unless block_given? addrs.each(&block) end end end if __FILE__ == $0 puts "hostname: #{Sys::Host::hostname}" puts "IP addresses: #{Sys::Host::ip_addr.join(', ')}" Sys::Host::ip_addr{|i| puts "==> #{i}"} end -- Gotoken