class Resolv::Hosts - Documentation for Ruby 4.0
Description: class Resolv::Hosts: Hosts is a hostname resolver that uses the system hosts file.
Keywords: ruby,class,Resolv::Hosts

🔍

No recent searches

# class Resolv::Hosts

`Resolv::Hosts` is a hostname resolver that uses the system hosts file.

### Constants

DefaultFileName

  The default file name for host names

### Public Class Methods

new (filename = DefaultFileName)
Source

    # File lib/resolv.rb, line 195
    def initialize(filename = DefaultFileName)
      @filename = filename
      @mutex = Thread::Mutex.new
      @initialized = nil
    end

Creates a new `Resolv::Hosts`, using `filename` for its data source.

### Public Instance Methods

each_address (name, &proc)
Source

    # File lib/resolv.rb, line 242
    def each_address(name, &proc)
      lazy_initialize
      @name2addr[name]&.each(&proc)
    end

Iterates over all IP addresses for `name` retrieved from the hosts file.

each_name (address, &proc)
Source

    # File lib/resolv.rb, line 267
    def each_name(address, &proc)
      lazy_initialize
      @addr2name[address]&.each(&proc)
    end

Iterates over all hostnames for `address` retrieved from the hosts file.

getaddress (name)
Source

    # File lib/resolv.rb, line 225
    def getaddress(name)
      each_address(name) {|address| return address}
      raise ResolvError.new("#{@filename} has no name: #{name}")
    end

Gets the IP address of `name` from the hosts file.

getaddresses (name)
Source

    # File lib/resolv.rb, line 233
    def getaddresses(name)
      ret = []
      each_address(name) {|address| ret << address}
      return ret
    end

Gets all IP addresses for `name` from the hosts file.

getname (address)
Source

    # File lib/resolv.rb, line 250
    def getname(address)
      each_name(address) {|name| return name}
      raise ResolvError.new("#{@filename} has no address: #{address}")
    end

Gets the hostname of `address` from the hosts file.

getnames (address)
Source

    # File lib/resolv.rb, line 258
    def getnames(address)
      ret = []
      each_name(address) {|name| ret << name}
      return ret
    end

Gets all hostnames for `address` from the hosts file.