Class: Gem::Resolv::IPv4

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/vendor/resolv/lib/resolv.rb

Overview

A Gem::Resolv::DNS IPv4 address.

Constant Summary collapse

Regex256 =

Regular expression IPv4 addresses must match.

/0
|1(?:[0-9][0-9]?)?
|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
|[3-9][0-9]?/x
Regex =
/\A(#{Regex256})\.(#{Regex256})\.(#{Regex256})\.(#{Regex256})\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

:nodoc:



2914
2915
2916
2917
2918
2919
2920
2921
2922
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2914

def initialize(address) # :nodoc:
  unless address.kind_of?(String)
    raise ArgumentError, 'IPv4 address must be a string'
  end
  unless address.length == 4
    raise ArgumentError, "IPv4 address expects 4 bytes but #{address.length} bytes"
  end
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

The raw IPv4 address as a String.



2930
2931
2932
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2930

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object



2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2896

def self.create(arg)
  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

:nodoc:



2948
2949
2950
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2948

def ==(other) # :nodoc:
  return @address == other.address
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


2952
2953
2954
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2952

def eql?(other) # :nodoc:
  return self == other
end

#hashObject

:nodoc:



2956
2957
2958
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2956

def hash # :nodoc:
  return @address.hash
end

#inspectObject

:nodoc:



2936
2937
2938
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2936

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

#to_nameObject

Turns this IPv4 address into a Gem::Resolv::DNS::Name.



2943
2944
2945
2946
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2943

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

#to_sObject

:nodoc:



2932
2933
2934
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 2932

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