Class: MACAddr

Inherits:
Object
  • Object
show all
Defined in:
lib/netutils/macaddr.rb

Constant Summary collapse

NBBY =
8
Length =
6

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ MACAddr

Returns a new instance of MACAddr.



5
6
7
8
9
10
11
# File 'lib/netutils/macaddr.rb', line 5

def initialize(s)
	s = s.delete('-.:')
	raise if s =~ /[^0-9a-z]/i	# XXX EINVAL
	v = s.hex
	raise if v > 0xffffffffffff	# XXX ERANGE
	@addr = v
end

Instance Method Details

#[](n, len) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/netutils/macaddr.rb', line 13

def [](n, len)
	raise if len > Length
	raise if Length % len != 0
	n *= len
	v = 0
	off = 0
	while off < len do
		v <<= NBBY
		v |= (@addr >> (NBBY * ((Length - 1) - n - off))) & 0xff
		off += 1
	end
	return v
end

#to_s(sep = '.', step = 2) ⇒ Object



38
39
40
# File 'lib/netutils/macaddr.rb', line 38

def to_s(sep = '.', step = 2)
	to_a(step).map { |v| sprintf('%0*x', step * 2, v) }.join(sep)
end