lib/macaddr.rb in macaddr-1.2.1 vs lib/macaddr.rb in macaddr-1.3.0

- old
+ new

@@ -21,11 +21,11 @@ end require 'systemu' module Mac - VERSION = '1.2.1' + VERSION = '1.3.0' def Mac.version ::Mac::VERSION end @@ -56,34 +56,41 @@ re = %r/[^:\-](?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F][^:\-]/io cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all' null = test(?e, '/dev/null') ? '/dev/null' : 'NUL' - lines = nil + output = nil cmds.each do |cmd| status, stdout, stderr = systemu(cmd) rescue next next unless stdout and stdout.size > 0 - lines = stdout.split(/\n/) and break + output = stdout and break end - raise "all of #{ cmds.join ' ' } failed" unless lines + raise "all of #{ cmds.join ' ' } failed" unless output - candidates = lines.select{|line| line =~ re} + @mac_address = parse(output) + end + + def parse(output) + lines = output.split(/\n/) + + candidates = lines.select{|line| line =~ RE} raise 'no mac address candidates' unless candidates.first - candidates.map!{|c| c[re].strip} + candidates.map!{|c| c[RE].strip} maddr = candidates.first raise 'no mac address found' unless maddr maddr.strip! maddr.instance_eval{ @list = candidates; def list() @list end } - - @mac_address = maddr + maddr end ## # Shorter alias for #address alias_method "addr", "address" end + + RE = %r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io end -Macaddr = Mac +MacAddr = Macaddr = Mac