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.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#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 |
# File 'lib/pio/ipv4_address.rb', line 25 def initialize(addr) @value = case addr when Integer IPAddr.new(addr, Socket::AF_INET) when String IPAddr.new(addr) else addr.value 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.
94 95 96 |
# File 'lib/pio/ipv4_address.rb', line 94 def class_a? mask(1).to_s == '0.0.0.0' end |
#class_b? ⇒ bool
Returns true if the address belongs to class B.
100 101 102 |
# File 'lib/pio/ipv4_address.rb', line 100 def class_b? mask(2).to_s == '128.0.0.0' end |
#class_c? ⇒ bool
Returns true if the address belongs to class C.
106 107 108 |
# File 'lib/pio/ipv4_address.rb', line 106 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.
112 113 114 |
# File 'lib/pio/ipv4_address.rb', line 112 def class_d? mask(4).to_s == '224.0.0.0' end |
#class_e? ⇒ bool
Returns true if the address belongs to class E.
119 120 121 |
# File 'lib/pio/ipv4_address.rb', line 119 def class_e? mask(4).to_s == '240.0.0.0' end |
#eql?(other) ⇒ Boolean
47 48 49 |
# File 'lib/pio/ipv4_address.rb', line 47 def eql?(other) @value == other.value end |
#hash ⇒ Object
51 52 53 |
# File 'lib/pio/ipv4_address.rb', line 51 def hash to_s.hash end |
#mask(masklen) ⇒ IPv4Address Also known as: prefix
Returns the IPv4 address masked with masklen
.
87 88 89 |
# File 'lib/pio/ipv4_address.rb', line 87 def mask(masklen) clone.mask!(masklen) end |
#mask!(masklen) ⇒ IPv4Address Also known as: prefix!
Returns the IPv4 address masked with masklen
.
79 80 81 82 |
# File 'lib/pio/ipv4_address.rb', line 79 def mask!(masklen) @value = @value.mask(masklen) self end |
#prefixlen ⇒ Number
Returns prefix length of IPv4 address.
56 57 58 59 60 61 62 63 |
# File 'lib/pio/ipv4_address.rb', line 56 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.
67 68 69 |
# File 'lib/pio/ipv4_address.rb', line 67 def to_a to_s.split('.').map(&:to_i) end |
#to_ary ⇒ Array
Returns an array of decimal numbers converted from IPv4 address.
73 74 75 |
# File 'lib/pio/ipv4_address.rb', line 73 def to_ary to_a end |
#unicast? ⇒ bool
Returns true if the address is unicast address.
125 126 127 |
# File 'lib/pio/ipv4_address.rb', line 125 def unicast? class_a? || class_b? || class_c? end |