class Resolv - Documentation for Ruby 3.4
Description: class Resolv: Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can handle multiple DNS requests concurrently without blocking the entire Ruby i
Keywords: ruby,class,Resolv

# class Resolv

`Resolv` is a thread-aware `DNS` resolver library written in Ruby. `Resolv` can handle multiple `DNS` requests concurrently without blocking the entire Ruby interpreter.

See also resolv-replace.rb to replace the libc resolver with `Resolv`.

`Resolv` can look up various `DNS` resources using the `DNS` module directly.

Examples:

    p Resolv.getaddress "www.ruby-lang.org"
    p Resolv.getname "210.251.121.214"

    Resolv::DNS.open do |dns|
      ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
      p ress.map(&:address)
      ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
      p ress.map { |r| [r.exchange.to_s, r.preference] }
    end

## Bugs¶ ↑

- NIS is not supported.

- /etc/nsswitch.conf is not supported.

### Constants

AddressRegex

  Address `Regexp` to use for matching IP addresses.

DefaultResolver

  Default resolver to use for `Resolv` class methods.

VERSION

  The version string

### Public Class Methods

each_address (name, &block)
Source

    # File lib/resolv.rb, line 57
    def self.each_address(name, &block)
      DefaultResolver.each_address(name, &block)
    end

Iterates over all IP addresses for `name`.

each_name (address, &proc)
Source

    # File lib/resolv.rb, line 78
    def self.each_name(address, &proc)
      DefaultResolver.each_name(address, &proc)
    end

Iterates over all hostnames for `address`.

getaddress (name)
Source

    # File lib/resolv.rb, line 43
    def self.getaddress(name)
      DefaultResolver.getaddress(name)
    end

Looks up the first IP address for `name`.

getaddresses (name)
Source

    # File lib/resolv.rb, line 50
    def self.getaddresses(name)
      DefaultResolver.getaddresses(name)
    end

Looks up all IP address for `name`.

getname (address)
Source

    # File lib/resolv.rb, line 64
    def self.getname(address)
      DefaultResolver.getname(address)
    end

Looks up the hostname of `address`.

getnames (address)
Source

    # File lib/resolv.rb, line 71
    def self.getnames(address)
      DefaultResolver.getnames(address)
    end

Looks up all hostnames for `address`.

new (resolvers=(arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil))
Source

    # File lib/resolv.rb, line 89
    def initialize(resolvers=(arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil))
      if !keyword_not_set && !arg_not_set
        warn "Support for separate use_ipv6 keyword is deprecated, as it is ignored if an argument is provided. Do not provide a positional argument if using the use_ipv6 keyword argument.", uplevel: 1
      end

      @resolvers = case resolvers
      when Hash, nil
        [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(resolvers || {}))]
      else
        resolvers
      end
    end

Creates a new `Resolv` using `resolvers`.

If `resolvers` is not given, a hash, or `nil`, uses a `Hosts` resolver and and a `DNS` resolver. If `resolvers` is a hash, uses the hash as configuration for the `DNS` resolver.

### Public Instance Methods

each_address (name) { |name| ... }
Source

    # File lib/resolv.rb, line 122
    def each_address(name)
      if AddressRegex =~ name
        yield name
        return
      end
      yielded = false
      @resolvers.each {|r|
        r.each_address(name) {|address|
          yield address.to_s
          yielded = true
        }
        return if yielded
      }
    end

Iterates over all IP addresses for `name`.

each_name (address) { |name| ... }
Source

    # File lib/resolv.rb, line 157
    def each_name(address)
      yielded = false
      @resolvers.each {|r|
        r.each_name(address) {|name|
          yield name.to_s
          yielded = true
        }
        return if yielded
      }
    end

Iterates over all hostnames for `address`.

getaddress (name)
Source

    # File lib/resolv.rb, line 105
    def getaddress(name)
      each_address(name) {|address| return address}
      raise ResolvError.new("no address for #{name}")
    end

Looks up the first IP address for `name`.

getaddresses (name)
Source

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

Looks up all IP address for `name`.

getname (address)
Source

    # File lib/resolv.rb, line 140
    def getname(address)
      each_name(address) {|name| return name}
      raise ResolvError.new("no name for #{address}")
    end

Looks up the hostname of `address`.

getnames (address)
Source

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

Looks up all hostnames for `address`.