Class: Dnsruby::IPv4
- Inherits:
-
Object
- Object
- Dnsruby::IPv4
- Defined in:
- lib/dnsruby/ipv4.rb
Constant Summary collapse
- Regex =
Regular expression IPv4 addresses must match
/\A(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\z/
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
A String representation of the IPv4 address.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(address) ⇒ IPv4
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
- #to_name ⇒ Object
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(address) ⇒ IPv4
:nodoc:
39 40 41 42 43 44 |
# File 'lib/dnsruby/ipv4.rb', line 39 def initialize(address) #:nodoc: unless address.kind_of?(String) && address.length == 4 raise ArgumentError.new('IPv4 address must be 4 bytes') end @address = address end |
Instance Attribute Details
#address ⇒ Object (readonly)
A String representation of the IPv4 address.
47 48 49 |
# File 'lib/dnsruby/ipv4.rb', line 47 def address @address end |
Class Method Details
.create(arg) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dnsruby/ipv4.rb', line 21 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
62 63 64 |
# File 'lib/dnsruby/ipv4.rb', line 62 def ==(other) return @address == other.address end |
#eql?(other) ⇒ Boolean
66 67 68 |
# File 'lib/dnsruby/ipv4.rb', line 66 def eql?(other) return self == other end |
#hash ⇒ Object
70 71 72 |
# File 'lib/dnsruby/ipv4.rb', line 70 def hash return @address.hash end |
#inspect ⇒ Object
:nodoc:
53 54 55 |
# File 'lib/dnsruby/ipv4.rb', line 53 def inspect #:nodoc: return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
57 58 59 60 |
# File 'lib/dnsruby/ipv4.rb', line 57 def to_name return Name.create( '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse) end |
#to_s ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/dnsruby/ipv4.rb', line 49 def to_s #:nodoc: return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC")) end |