Class: Net::Address::MAC

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/net/address/mac.rb

Instance Method Summary collapse

Constructor Details

#initialize(mac_address) ⇒ MAC

Returns a new instance of MAC.

Raises:

  • (ArgumentError)


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_aObject 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_iObject



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

Raises:

  • (ArgumentError)


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