Class: Dot11::MACAddress

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dot11/macaddress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ MACAddress

Returns a new instance of MACAddress.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dot11/macaddress.rb', line 7

def initialize(address)
  if address.kind_of?(Integer)
    @address = [address].pack("Q").unpack("C8")[0, 6].reverse
  elsif address.kind_of?(String) && address =~ /^(.*?)(?:\/(.*))?$/
    @prefix_length = $2.to_i
    
    @address = $1.split(":").map {|octet| octet.to_i(16)}
  elsif address.kind_of?(Array)
    @address = address
  end 
end

Instance Attribute Details

#prefix_lengthObject

Returns the value of attribute prefix_length.



5
6
7
# File 'lib/dot11/macaddress.rb', line 5

def prefix_length
  @prefix_length
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
# File 'lib/dot11/macaddress.rb', line 43

def ==(other)
  eql?(other)
end

#[](*index) ⇒ Object



35
36
37
# File 'lib/dot11/macaddress.rb', line 35

def [](*index)
  @address[*index]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dot11/macaddress.rb', line 39

def eql?(other)
  to_i == other.to_i
end

#hashObject



47
48
49
# File 'lib/dot11/macaddress.rb', line 47

def hash
  to_i.hash
end

#inspectObject



27
28
29
# File 'lib/dot11/macaddress.rb', line 27

def inspect
  to_s
end

#to_arrObject



31
32
33
# File 'lib/dot11/macaddress.rb', line 31

def to_arr
  @address
end

#to_iObject



19
20
21
# File 'lib/dot11/macaddress.rb', line 19

def to_i
  ("\x00\x00" + @address.pack("C6")).reverse.unpack("Q")[0]
end

#to_sObject



23
24
25
# File 'lib/dot11/macaddress.rb', line 23

def to_s
  @address.map{|byte| "%02x" % byte}.join(":") + (@prefix_length == 0 ? "" : "/#{@prefix_length}")
end