Class: Pio::Mac

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pio/mac.rb

Overview

Ethernet address (MAC address) class.

Defined Under Namespace

Classes: InvalidValueError

Converters collapse

Predicates collapse

Equality collapse

Debug collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Mac

Creates a Pio::Mac instance that encapsulates Ethernet addresses.

Examples:

address as a hexadecimal string

Mac.new("11:22:33:44:55:66")

address as a hexadecimal number

Mac.new(0xffffffffffff)

Parameters:

  • value (#to_str, #to_int)

    the value converted to an Ethernet address.



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
    raise 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.

Examples:

mac_address = Mac.new("11:22:33:44:55:66")

mac_address == Mac.new("11:22:33:44:55:66") #=> true
mac_address == "11:22:33:44:55:66" #=> true
mac_address == "INVALID_MAC_ADDRESS" #=> false

Parameters:

  • other (#to_s)

    a Pio::Mac object or an object that can be converted to an Ethernet address.

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/pio/mac.rb', line 159

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.

Examples:

Mac.new("ff:ff:ff:ff:ff:ff").broadcast? #=> true

Returns:

  • (Boolean)


121
122
123
# File 'lib/pio/mac.rb', line 121

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.

Examples:

fdb = {
  Mac.new("11:22:33:44:55:66") => 1,
  Mac.new("66:55:44:33:22:11") => 2
}

fdb[ Mac.new("11:22:33:44:55:66")] #=> 1
fdb["11:22:33:44:55:66"] #=> 1
fdb[0x112233445566] #=> 1

Returns:

  • (Boolean)

See Also:



182
183
184
# File 'lib/pio/mac.rb', line 182

def eql?(other)
  self == other
end

#inspectString

Returns a string containing a human-readable representation of Pio::Mac for debugging.

Returns:

  • (String)


196
197
198
# File 'lib/pio/mac.rb', line 196

def inspect
  %(#<#{self.class}:#{__id__} "#{self}">)
end

#multicast?Boolean

Returns true if Ethernet address is a multicast address.

Examples:

Mac.new("01:00:00:00:00:00").multicast? #=> true
Mac.new("00:00:00:00:00:00").multicast? #=> false

Returns:

  • (Boolean)


111
112
113
# File 'lib/pio/mac.rb', line 111

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.

Examples:

Mac.new("01:80:c2:00:00:00").reserved? #=> true
Mac.new("11:22:33:44:55:66").reserved? #=> false

Returns:

  • (Boolean)


135
136
137
# File 'lib/pio/mac.rb', line 135

def reserved?
  (to_i >> 8) == 0x0180c20000
end

#to_aArray

Returns an Array of decimal numbers converted from Ethernet’s address string format.

Examples:

Mac.new("11:22:33:44:55:66").to_a
#=> [0x11, 0x22, 0x33, 0x44, 0x55, 0x66]

Returns:

  • (Array)


92
93
94
# File 'lib/pio/mac.rb', line 92

def to_a
  to_s.split(':').map(&:hex)
end

#to_hexObject



96
97
98
# File 'lib/pio/mac.rb', line 96

def to_hex
  to_a.map(&:to_hex).join(', ')
end

#to_iInteger

Returns an Ethernet address in its numeric presentation.

Examples:

Mac.new("11:22:33:44:55:66").to_i #=> 18838586676582

Returns:



50
51
52
# File 'lib/pio/mac.rb', line 50

def to_i
  @value
end

#to_sString

Returns the Ethernet address as 6 pairs of hexadecimal digits delimited by colons.

Examples:

Mac.new(0x112233445566).to_s #=> "11:22:33:44:55:66"

Returns:

  • (String)


63
64
65
# File 'lib/pio/mac.rb', line 63

def to_s
  format('%012x', @value).unpack('a2' * 6).join(':')
end

#to_strString

Implicitly converts obj to a string.

Examples:

mac = Mac.new("11:22:33:44:55:66")
puts "MAC = " + mac #=> "MAC = 11:22:33:44:55:66"

Returns:

  • (String)

See Also:



78
79
80
# File 'lib/pio/mac.rb', line 78

def to_str
  to_s
end