Class: Net::Address::MAC
- Inherits:
-
Object
- Object
- Net::Address::MAC
- Includes:
- Comparable
- Defined in:
- lib/net/address/mac.rb
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(mac_address) ⇒ MAC
constructor
A new instance of MAC.
- #to_a ⇒ Object (also: #octets)
- #to_i ⇒ Object
- #to_s(separator = '.', step = 4) ⇒ Object
Constructor Details
#initialize(mac_address) ⇒ MAC
Returns a new instance of MAC.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/net/address/mac.rb', line 11 def initialize(mac_address) case mac_address when self.class @mac_address = mac_address.to_i when Integer @mac_address = mac_address.to_i when String @mac_address = clear(mac_address).hex when Array @mac_address = mac_address.join.hex else raise ArgumentError, "No implicit conversion of #{mac_address.class.name} into #{self.class.name}" end raise ArgumentError, "MAC Address out of range #{mac_address}" if @mac_address.to_i < 0 or @mac_address.to_i > 0xffffffffffff end |
Instance Method Details
#<=>(other) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/net/address/mac.rb', line 46 def <=>(other) begin other = self.class.new(other) unless other.is_a? self.class rescue ArgumentError => e raise ArgumentError, "No implicit conversion of #{other.class.name} into #{self.name}" end self.to_i <=> other.to_i end |
#to_a ⇒ Object Also known as: octets
39 40 41 42 |
# File 'lib/net/address/mac.rb', line 39 def to_a mac = '%012X' % @mac_address.to_i (0..5).map{|i| mac[i*2,2]} end |
#to_i ⇒ Object
35 36 37 |
# File 'lib/net/address/mac.rb', line 35 def to_i @mac_address.to_i end |
#to_s(separator = '.', step = 4) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/net/address/mac.rb', line 27 def to_s(separator='.', step = 4) step = step.to_i mac = '%012X' % @mac_address.to_i return mac if separator.nil? or step == 0 raise ArgumentError, "Wrong step value #{step}" unless [0,2,4].include? step.to_i (0..(mac.length-1)/step).map{|i|mac[i*step,step]}.join(separator.to_s) end |