Class: Pio::IPv4Address
- Inherits:
-
Object
- Object
- Pio::IPv4Address
- Extended by:
- Forwardable
- Defined in:
- lib/pio/ipv4_address.rb
Overview
IPv4 Address
Instance Attribute Summary collapse
-
#value ⇒ Range
readonly
Creates a Range object for the network address.
Instance Method Summary collapse
-
#class_a? ⇒ bool
Returns true if the address belongs to class A.
-
#class_b? ⇒ bool
Returns true if the address belongs to class B.
-
#class_c? ⇒ bool
Returns true if the address belongs to class C.
-
#class_d? ⇒ bool
(also: #multicast?)
Returns true if the address belongs to class D.
-
#class_e? ⇒ bool
Returns true if the address belongs to class E.
-
#initialize(addr) ⇒ IPv4Address
constructor
Creates a IPv4Address instance object as a proxy to IPAddr class.
-
#mask(masklen) ⇒ IPv4Address
(also: #prefix)
Returns the IPv4 address masked with
masklen. -
#mask!(masklen) ⇒ IPv4Address
(also: #prefix!)
Returns the IPv4 address masked with
masklen. -
#prefixlen ⇒ Number
Prefix length of IPv4 address.
-
#to_a ⇒ Array
An array of decimal numbers converted from IPv4 address.
-
#to_ary ⇒ Array
An array of decimal numbers converted from IPv4 address.
-
#unicast? ⇒ bool
Returns true if the address is unicast address.
Constructor Details
#initialize(addr) ⇒ IPv4Address
Creates a Pio::IPv4Address instance object as a proxy to IPAddr class.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pio/ipv4_address.rb', line 23 def initialize(addr) case addr when Integer @value = IPAddr.new(addr, Socket::AF_INET) when String @value = IPAddr.new(addr) else @value = addr.value end end |
Instance Attribute Details
#value ⇒ Range (readonly)
10 11 12 |
# File 'lib/pio/ipv4_address.rb', line 10 def value @value end |
Instance Method Details
#class_a? ⇒ bool
84 85 86 |
# File 'lib/pio/ipv4_address.rb', line 84 def class_a? mask(1).to_s == '0.0.0.0' end |
#class_b? ⇒ bool
90 91 92 |
# File 'lib/pio/ipv4_address.rb', line 90 def class_b? mask(2).to_s == '128.0.0.0' end |
#class_c? ⇒ bool
96 97 98 |
# File 'lib/pio/ipv4_address.rb', line 96 def class_c? mask(3).to_s == '192.0.0.0' end |
#class_d? ⇒ bool Also known as: multicast?
102 103 104 |
# File 'lib/pio/ipv4_address.rb', line 102 def class_d? mask(4).to_s == '224.0.0.0' end |
#class_e? ⇒ bool
109 110 111 |
# File 'lib/pio/ipv4_address.rb', line 109 def class_e? mask(4).to_s == '240.0.0.0' end |
#mask(masklen) ⇒ IPv4Address Also known as: prefix
Returns the IPv4 address masked with masklen.
77 78 79 |
# File 'lib/pio/ipv4_address.rb', line 77 def mask(masklen) clone.mask!(masklen) end |
#mask!(masklen) ⇒ IPv4Address Also known as: prefix!
Returns the IPv4 address masked with masklen.
69 70 71 72 |
# File 'lib/pio/ipv4_address.rb', line 69 def mask!(masklen) @value = @value.mask(masklen) self end |
#prefixlen ⇒ Number
46 47 48 49 50 51 52 53 |
# File 'lib/pio/ipv4_address.rb', line 46 def prefixlen netmask = to_range.first.to_i ^ to_range.last.to_i if netmask > 0 32 - format('%b', netmask).length else 32 end end |
#to_a ⇒ Array
57 58 59 |
# File 'lib/pio/ipv4_address.rb', line 57 def to_a to_s.split('.').map(&:to_i) end |
#to_ary ⇒ Array
63 64 65 |
# File 'lib/pio/ipv4_address.rb', line 63 def to_ary to_a end |
#unicast? ⇒ bool
115 116 117 |
# File 'lib/pio/ipv4_address.rb', line 115 def unicast? class_a? || class_b? || class_c? end |