Class: Pio::Mac
Overview
Ethernet address (MAC address) class.
Defined Under Namespace
Classes: InvalidValueError
Converters collapse
-
#to_a ⇒ Array
Returns an Array of decimal numbers converted from Ethernet’s address string format.
-
#to_i ⇒ Integer
Returns an Ethernet address in its numeric presentation.
-
#to_s ⇒ String
Returns the Ethernet address as 6 pairs of hexadecimal digits delimited by colons.
-
#to_str ⇒ String
Implicitly converts
obj
to a string.
Predicates collapse
-
#broadcast? ⇒ Boolean
Returns true if Ethernet address is a broadcast address.
-
#multicast? ⇒ Boolean
Returns true if Ethernet address is a multicast address.
-
#reserved? ⇒ Boolean
Returns
true
if Ethernet address is an IEEE 802.1D or 802.1Q reserved address.
Equality collapse
-
#==(other) ⇒ Boolean
Returns
true
ifother
can be converted to a Mac object and its numeric representation is equal toobj
‘s. -
#eql?(other) ⇒ Boolean
Returns
true
ifobj
andother
refer to the same hash key.
Debug collapse
-
#inspect ⇒ String
Returns a string containing a human-readable representation of Mac for debugging.
Instance Method Summary collapse
-
#initialize(value) ⇒ Mac
constructor
Creates a Mac instance that encapsulates Ethernet addresses.
Constructor Details
#initialize(value) ⇒ Mac
Creates a Pio::Mac instance that encapsulates Ethernet addresses.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pio/mac.rb', line 27 def initialize(value) if value.respond_to?(:to_str) @value = parse_mac_string(value.to_str) elsif value.respond_to?(:to_int) @value = value.to_int validate_value_range else fail TypeError end rescue ArgumentError, TypeError raise InvalidValueError, "Invalid MAC address: #{ value.inspect }" end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true
if other
can be converted to a Pio::Mac object and its numeric representation is equal to obj
‘s.
158 159 160 161 162 |
# File 'lib/pio/mac.rb', line 158 def ==(other) to_i == Mac.new(other).to_i rescue InvalidValueError false end |
#broadcast? ⇒ Boolean
Returns true if Ethernet address is a broadcast address.
119 120 121 |
# File 'lib/pio/mac.rb', line 119 def broadcast? to_a.all? { | each | each == 0xff } end |
#eql?(other) ⇒ Boolean
Returns true
if obj
and other
refer to the same hash key. #== is used for the comparison.
180 181 182 |
# File 'lib/pio/mac.rb', line 180 def eql?(other) self == other end |
#inspect ⇒ String
Returns a string containing a human-readable representation of Pio::Mac for debugging.
194 195 196 |
# File 'lib/pio/mac.rb', line 194 def inspect %(#<#{self.class}:#{__id__} "#{self}">) end |
#multicast? ⇒ Boolean
Returns true if Ethernet address is a multicast address.
109 110 111 |
# File 'lib/pio/mac.rb', line 109 def multicast? to_a[0] & 1 == 1 end |
#reserved? ⇒ Boolean
Returns true
if Ethernet address is an IEEE 802.1D or 802.1Q reserved address. See standards.ieee.org/develop/regauth/grpmac/public.html for details.
133 134 135 |
# File 'lib/pio/mac.rb', line 133 def reserved? ( to_i >> 8) == 0x0180c20000 end |
#to_a ⇒ Array
Returns an Array of decimal numbers converted from Ethernet’s address string format.
92 93 94 95 96 |
# File 'lib/pio/mac.rb', line 92 def to_a to_s.split(':').map do | each | each.hex end end |
#to_i ⇒ Integer
Returns an Ethernet address in its numeric presentation.
50 51 52 |
# File 'lib/pio/mac.rb', line 50 def to_i @value end |
#to_s ⇒ String
Returns the Ethernet address as 6 pairs of hexadecimal digits delimited by colons.
63 64 65 |
# File 'lib/pio/mac.rb', line 63 def to_s format('%012x', @value).unpack('a2' * 6).join(':') end |
#to_str ⇒ String
Implicitly converts obj
to a string.
78 79 80 |
# File 'lib/pio/mac.rb', line 78 def to_str to_s end |