Resolv.[getaddress] should fail gracefully on OSX when offline
When OSX isn't connected to the internet, Resolv.getaddress fails abruptly due to a NoMethodError in Resolv::DNS::Config.lazy_initialize.
On OSX, /etc/resolv.conf exists only when it is connected to a network. When OSX is not (or unable) connected to any network, /etc/resolv.conf does not exist.
The cause of the NoMethodError is due to return of an uninitialized variable named "config_hash" in Resolv::DNS::Config.default_config_hash. Its only callee "lazy_initialize", expects an empty hash to be returned instead of "nil" on parsing errors (e.g. when /etc/resolv.conf does not exist).
The fix is simply to move the initialization of "config_hash" to the beginning of the function. Please see the patch attached.
Hope this helps.
When OSX disconnected from internet:
irb(main):001:0> load "/opt/local/lib/ruby/1.9.1/resolv.rb"
irb(main):002:0> Resolv.getaddress "www.ruby-lang.org"
NoMethodError: undefined method
include?' for nil:NilClassblock in lazy_initialize'
block in lazy_initialize'synchronize'
each_address'block in each_address'
from /opt/local/bin/irb:12:in `'
When OSX connected to internet: