I don't think Ruby has a standard api for this, but under some assumptions this should be fairly reliable on different platforms:
require 'socket' Socket::getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET).map { |x| x[3] }
Here we take a lot of things like a machine with a local host name pointing to the correct IP addresses. Thus, it is definitely not completely reliable, but it is platform independent and works with general settings.
Edit: if you decide to move on to ifconfig analysis, consider forking ruby-ifconfig . He claims to do this on most non-Windows platforms.
anshul
source share