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.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pio/ipv4_address.rb', line 25 def initialize(addr) case addr when Integer @value = IPAddr.new(addr, Socket::AF_INET) when String @value = IPAddr.new(addr) when IPv4Address @value = addr.value else fail TypeError, "Invalid IPv4 address: #{ addr.inspect }" end end |
Instance Attribute Details
#value ⇒ Range (readonly)
Returns Creates a Range object for the network address.
12 13 14 |
# File 'lib/pio/ipv4_address.rb', line 12 def value @value end |
Instance Method Details
#class_a? ⇒ bool
Returns true if the address belongs to class A.
90 91 92 |
# File 'lib/pio/ipv4_address.rb', line 90 def class_a? mask(1).to_s == '0.0.0.0' end |
#class_b? ⇒ bool
Returns true if the address belongs to class B.
96 97 98 |
# File 'lib/pio/ipv4_address.rb', line 96 def class_b? mask(2).to_s == '128.0.0.0' end |
#class_c? ⇒ bool
Returns true if the address belongs to class C.
102 103 104 |
# File 'lib/pio/ipv4_address.rb', line 102 def class_c? mask(3).to_s == '192.0.0.0' end |
#class_d? ⇒ bool Also known as: multicast?
Returns true if the address belongs to class D.
108 109 110 |
# File 'lib/pio/ipv4_address.rb', line 108 def class_d? mask(4).to_s == '224.0.0.0' end |
#class_e? ⇒ bool
Returns true if the address belongs to class E.
115 116 117 |
# File 'lib/pio/ipv4_address.rb', line 115 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
.
83 84 85 |
# File 'lib/pio/ipv4_address.rb', line 83 def mask(masklen) clone.mask!(masklen) end |
#mask!(masklen) ⇒ IPv4Address Also known as: prefix!
Returns the IPv4 address masked with masklen
.
75 76 77 78 |
# File 'lib/pio/ipv4_address.rb', line 75 def mask!(masklen) @value = @value.mask(masklen) self end |
#prefixlen ⇒ Number
Returns prefix length of IPv4 address.
50 51 52 53 54 55 56 57 |
# File 'lib/pio/ipv4_address.rb', line 50 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
Returns an array of decimal numbers converted from IPv4 address.
61 62 63 64 65 |
# File 'lib/pio/ipv4_address.rb', line 61 def to_a to_s.split('.').map do | each | each.to_i end end |
#to_ary ⇒ Array
Returns an array of decimal numbers converted from IPv4 address.
69 70 71 |
# File 'lib/pio/ipv4_address.rb', line 69 def to_ary to_a end |
#unicast? ⇒ bool
Returns true if the address is unicast address.
121 122 123 |
# File 'lib/pio/ipv4_address.rb', line 121 def unicast? class_a? || class_b? || class_c? end |