In Files

  • resolv.rb

Resolv::Hosts

Constants

DefaultFileName

Public Class Methods

new(filename = DefaultFileName) click to toggle source
 
               # File resolv.rb, line 293
def initialize(filename = DefaultFileName)
  @filename = filename
  @mutex = Mutex.new
  @initialized = nil
end
            

Public Instance Methods

each_address(name, &proc) click to toggle source
 
               # File resolv.rb, line 341
def each_address(name, &proc)
  lazy_initialize
  if @name2addr.include?(name)
    @name2addr[name].each(&proc)
  end
end
            
each_name(address, &proc) click to toggle source
 
               # File resolv.rb, line 359
def each_name(address, &proc)
  lazy_initialize
  if @addr2name.include?(address)
    @addr2name[address].each(&proc)
  end
end
            
getaddress(name) click to toggle source
 
               # File resolv.rb, line 330
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("#{@filename} has no name: #{name}")
end
            
getaddresses(name) click to toggle source
 
               # File resolv.rb, line 335
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end
            
getname(address) click to toggle source
 
               # File resolv.rb, line 348
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("#{@filename} has no address: #{address}")
end
            
getnames(address) click to toggle source
 
               # File resolv.rb, line 353
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end
            
lazy_initialize() click to toggle source
 
               # File resolv.rb, line 299
def lazy_initialize
  @mutex.synchronize {
    unless @initialized
      @name2addr = {}
      @addr2name = {}
      open(@filename) {|f|
        f.each {|line|
          line.sub!(/#.*/, '')
          addr, hostname, *aliases = line.split(/\s+/)
          next unless addr
          addr.untaint
          hostname.untaint
          @addr2name[addr] = [] unless @addr2name.include? addr
          @addr2name[addr] << hostname
          @addr2name[addr] += aliases
          @name2addr[hostname] = [] unless @name2addr.include? hostname
          @name2addr[hostname] << addr
          aliases.each {|n|
            n.untaint
            @name2addr[n] = [] unless @name2addr.include? n
            @name2addr[n] << addr
          }
        }
      }
      @name2addr.each {|name, arr| arr.reverse!}
      @initialized = true
    end
  }
  self
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus