Class: Resolv::IPv4

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

Overview

A Resolv::DNS IPv4 address.

Constant Summary collapse

Regex256 =
/0
|1(?:[0-9][0-9]?)?
|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
|[3-9][0-9]?/x
Regex =

Regular expression IPv4 addresses must match.

/\A(#{Regex256})\.(#{Regex256})\.(#{Regex256})\.(#{Regex256})\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

:nodoc:



3051
3052
3053
3054
3055
3056
3057
3058
3059
# File 'lib/resolv.rb', line 3051

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.



3067
3068
3069
# File 'lib/resolv.rb', line 3067

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object

Creates a new IPv4 address from arg which may be:

IPv4

returns arg.

String

arg must match the IPv4::Regex constant



3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
# File 'lib/resolv.rb', line 3033

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:



3085
3086
3087
# File 'lib/resolv.rb', line 3085

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3089
3090
3091
# File 'lib/resolv.rb', line 3089

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

#hashObject

:nodoc:



3093
3094
3095
# File 'lib/resolv.rb', line 3093

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

#inspectObject

:nodoc:



3073
3074
3075
# File 'lib/resolv.rb', line 3073

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

#to_nameObject

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



3080
3081
3082
3083
# File 'lib/resolv.rb', line 3080

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

#to_sObject

:nodoc:



3069
3070
3071
# File 'lib/resolv.rb', line 3069

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