Skip to content

Instantly share code, notes, and snippets.

@taylor
Created September 15, 2011 05:10
Show Gist options
  • Select an option

  • Save taylor/1218594 to your computer and use it in GitHub Desktop.

Select an option

Save taylor/1218594 to your computer and use it in GitHub Desktop.
mac="0800273B51A9"
# With reject
mac.split(/(..)/).reject {|c| c.empty?}.join(':')
# With Inject
mac.split(//).inject([]) do |nmac, e|
if x.nil? or x>0
nmac[nmac.size-1] = nmac[nmac.size-1] + e
x=0
else
nmac[nmac.size] = e
x=x+1
end
nmac
end.join(':')
mac.split(//).inject([]) { |m, e| if x.nil? or x>0 ;
m[m.size-1] = m[m.size-1] + e ; x=0 ; else ; m[m.size] = e ;
x=x+1 ; end ; m }.join(':')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment