Ruby 使用 getname 方法获取 IP 地址对应的主机名

require 'resolv'

ip = "192.0.34.166"

begin
  puts Resolv.getname(ip)
rescue
  puts "No hostname associated with #{ip}"
end

编程技巧