class Net::Address::MAC

Public Class Methods

new(mac_address) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# 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
octets()
Alias for: to_a
to_a() click to toggle source
# 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
Also aliased as: octets
to_i() click to toggle source
# File lib/net/address/mac.rb, line 35
def to_i
  @mac_address.to_i
end
to_s(separator='.', step = 4) click to toggle source
# 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

Private Instance Methods

clear(mac_address) click to toggle source
# File lib/net/address/mac.rb, line 57
def clear(mac_address)
  mac_address.to_s.gsub(/(:|-|\.|,|"|)/,'').gsub(/\s+/, '')
end