Kavincat Kavincat - 8 days ago 6
Ruby Question

How do I convert MAC address to formatted MAC address

Input: 009CFAEEB779

Output:00:9C:FA:EE:B7:79

I was searching for the method but didn't find. Is there any method? or I need to loop(map) through it and add the colon.

Answer

Do more research before posting on SO and don't include Rails tags unless the problem relates to web programming using Rails methods.

arr = "009CFAEEB779".scan(/\w{2}/)
=> ["00", "9C", "FA", "EE", "B7", "79"]
arr.join(':')
=> "00:9C:FA:EE:B7:79"

or shorter

"009CFAEEB779".scan(/\w{2}/).join(':')
=> "00:9C:FA:EE:B7:79"