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 string 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.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pio/mac.rb', line 25 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 string representation is equal to obj
‘s.
153 154 155 156 157 158 |
# File 'lib/pio/mac.rb', line 153 def ==(other) return false if other.is_a?(Integer) to_s == Mac.new(other).to_s rescue InvalidValueError false end |
#broadcast? ⇒ Boolean
Returns true if Ethernet address is a broadcast address.
115 116 117 |
# File 'lib/pio/mac.rb', line 115 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.
176 177 178 |
# File 'lib/pio/mac.rb', line 176 def eql?(other) self == other end |
#inspect ⇒ String
Returns a string containing a human-readable representation of Pio::Mac for debugging.
190 191 192 |
# File 'lib/pio/mac.rb', line 190 def inspect %(#<#{self.class}:#{__id__} "#{self}">) end |
#multicast? ⇒ Boolean
Returns true if Ethernet address is a multicast address.
105 106 107 |
# File 'lib/pio/mac.rb', line 105 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.
129 130 131 |
# File 'lib/pio/mac.rb', line 129 def reserved? (to_i >> 8) == 0x0180c20000 end |
#to_a ⇒ Array
Returns an Array of decimal numbers converted from Ethernet’s address string format.
90 91 92 |
# File 'lib/pio/mac.rb', line 90 def to_a to_s.split(':').map(&:hex) end |
#to_i ⇒ Integer
Returns an Ethernet address in its numeric presentation.
48 49 50 |
# File 'lib/pio/mac.rb', line 48 def to_i @value end |
#to_s ⇒ String
Returns the Ethernet address as 6 pairs of hexadecimal digits delimited by colons.
61 62 63 |
# File 'lib/pio/mac.rb', line 61 def to_s format('%012x', @value).unpack('a2' * 6).join(':') end |
#to_str ⇒ String
Implicitly converts obj
to a string.
76 77 78 |
# File 'lib/pio/mac.rb', line 76 def to_str to_s end |