Class: Resolv::IPv4
- Inherits:
-
Object
- Object
- Resolv::IPv4
- Defined in:
- lib/resolv.rb
Constant Summary collapse
- Regex =
/\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(address) ⇒ IPv4
constructor
A new instance of IPv4.
- #inspect ⇒ Object
- #to_name ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(address) ⇒ IPv4
Returns a new instance of IPv4.
1735 1736 1737 1738 1739 1740 |
# File 'lib/resolv.rb', line 1735 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
#address ⇒ Object (readonly)
Returns the value of attribute address.
1741 1742 1743 |
# File 'lib/resolv.rb', line 1741 def address @address end |
Class Method Details
.create(arg) ⇒ Object
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 |
# File 'lib/resolv.rb', line 1717 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
1756 1757 1758 |
# File 'lib/resolv.rb', line 1756 def ==(other) return @address == other.address end |
#eql?(other) ⇒ Boolean
1760 1761 1762 |
# File 'lib/resolv.rb', line 1760 def eql?(other) return self == other end |
#hash ⇒ Object
1764 1765 1766 |
# File 'lib/resolv.rb', line 1764 def hash return @address.hash end |
#inspect ⇒ Object
1747 1748 1749 |
# File 'lib/resolv.rb', line 1747 def inspect return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
1751 1752 1753 1754 |
# File 'lib/resolv.rb', line 1751 def to_name return DNS::Name.create( '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse) end |
#to_s ⇒ Object
1743 1744 1745 |
# File 'lib/resolv.rb', line 1743 def to_s return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC")) end |