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.
1781 1782 1783 1784 1785 1786 |
# File 'lib/resolv.rb', line 1781 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
1787 1788 1789 |
# File 'lib/resolv.rb', line 1787 def address @address end |
Class Method Details
.create(arg) ⇒ Object
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 |
# File 'lib/resolv.rb', line 1763 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
1802 1803 1804 |
# File 'lib/resolv.rb', line 1802 def ==(other) return @address == other.address end |
#eql?(other) ⇒ Boolean
1806 1807 1808 |
# File 'lib/resolv.rb', line 1806 def eql?(other) return self == other end |
#hash ⇒ Object
1810 1811 1812 |
# File 'lib/resolv.rb', line 1810 def hash return @address.hash end |
#inspect ⇒ Object
1793 1794 1795 |
# File 'lib/resolv.rb', line 1793 def inspect return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
1797 1798 1799 1800 |
# File 'lib/resolv.rb', line 1797 def to_name return DNS::Name.create( '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse) end |
#to_s ⇒ Object
1789 1790 1791 |
# File 'lib/resolv.rb', line 1789 def to_s return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC")) end |