Get your local IP address

Language: Ruby

# Origin: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/#viewSource

require 'socket'

def local_ip
  # turn off reverse DNS resolution temporarily
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true

  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end
Reveal More
Added about 2 years ago by Twitterprofilephoto_normal zh