Method: MacAddress#initialize

Defined in:
lib/mac_address.rb

#initialize(str, options = {}) ⇒ MacAddress

Returns a new instance of MacAddress.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mac_address.rb', line 5

def initialize(str, options = {})
  @mac_str = str.strip.dequote.strip

  if options[:strict] && !MacAddress.validate_strict(@mac_str)
    raise_argument_error(@mac_str)
  end

  n = @mac_str.index(':')
  if not n.nil? and n >= 12
    @mac_str = @mac_str.split(':')[0]
  end
  @mac_str = @mac_str.downcase.gsub(/^0[xX]/,'').gsub(/[^0-9a-f]/,'')

  raise ArgumentError.new("Invalid MAC address: #{str}") if @mac_str.length != 12
end