Class: Resolv::IPv4

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/resolv.rb

Overview

Obsoleted by ipaddr.rb?

Constant Summary collapse

Regex =

:nodoc:

/\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

Returns a new instance of IPv4.



1862
1863
1864
1865
1866
1867
# File 'lib/net/dns/resolv.rb', line 1862

def initialize(address)
  unless address.kind_of?(String) && address.length == 4
    raise ArgumentError.new('IPv4 address must be 4 bytes')
  end
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



1868
1869
1870
# File 'lib/net/dns/resolv.rb', line 1868

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object



1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
# File 'lib/net/dns/resolv.rb', line 1841

def self.create(arg)
  if(arg.kind_of?(String) && arg.length == 4)
    return self.new(arg)
  end
  case arg
  when IPv4
    return arg
  when Regex
    if (0..255) === (a = $1.to_i) &&
       (0..255) === (b = $2.to_i) &&
       (0..255) === (c = $3.to_i) &&
       (0..255) === (d = $4.to_i)
      return self.new([a, b, c, d].pack("CCCC"))
    else
      raise ArgumentError.new("IPv4 address with invalid value: " + arg)
    end
  else
    raise ArgumentError.new("cannot interpret as IPv4 address: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object



1883
1884
1885
# File 'lib/net/dns/resolv.rb', line 1883

def ==(other)
  return @address == other.address
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1887
1888
1889
# File 'lib/net/dns/resolv.rb', line 1887

def eql?(other)
  return self == other
end

#hashObject



1891
1892
1893
# File 'lib/net/dns/resolv.rb', line 1891

def hash
  return @address.hash
end

#inspectObject



1874
1875
1876
# File 'lib/net/dns/resolv.rb', line 1874

def inspect
  return "#<#{self.class} #{self.to_s}>"
end

#to_nameObject



1878
1879
1880
1881
# File 'lib/net/dns/resolv.rb', line 1878

def to_name
  return DNS::Name.create(
    '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse)
end

#to_sObject



1870
1871
1872
# File 'lib/net/dns/resolv.rb', line 1870

def to_s
  return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC"))
end