Class: Resolv::IPv4
- Inherits:
-
Object
- Object
- Resolv::IPv4
- Defined in:
- lib/resolv.rb
Overview
A 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
-
#address ⇒ Object
readonly
The raw IPv4 address as a String.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#eql?(other) ⇒ Boolean
:nodoc:.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(address) ⇒ IPv4
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#to_name ⇒ Object
Turns this IPv4 address into a Resolv::DNS::Name.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(address) ⇒ IPv4
:nodoc:
2192 2193 2194 2195 2196 2197 2198 2199 2200 |
# File 'lib/resolv.rb', line 2192 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
#address ⇒ Object (readonly)
The raw IPv4 address as a String.
2208 2209 2210 |
# File 'lib/resolv.rb', line 2208 def address @address end |
Class Method Details
.create(arg) ⇒ Object
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 |
# File 'lib/resolv.rb', line 2174 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:
2226 2227 2228 |
# File 'lib/resolv.rb', line 2226 def ==(other) # :nodoc: return @address == other.address end |
#eql?(other) ⇒ Boolean
:nodoc:
2230 2231 2232 |
# File 'lib/resolv.rb', line 2230 def eql?(other) # :nodoc: return self == other end |
#hash ⇒ Object
:nodoc:
2234 2235 2236 |
# File 'lib/resolv.rb', line 2234 def hash # :nodoc: return @address.hash end |
#inspect ⇒ Object
:nodoc:
2214 2215 2216 |
# File 'lib/resolv.rb', line 2214 def inspect # :nodoc: return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
Turns this IPv4 address into a Resolv::DNS::Name.
2221 2222 2223 2224 |
# File 'lib/resolv.rb', line 2221 def to_name return DNS::Name.create( '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse) end |
#to_s ⇒ Object
:nodoc:
2210 2211 2212 |
# File 'lib/resolv.rb', line 2210 def to_s # :nodoc: return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC")) end |