Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Created July 14, 2011 01:24
Show Gist options
  • Save ryanbriones/1081694 to your computer and use it in GitHub Desktop.
Save ryanbriones/1081694 to your computer and use it in GitHub Desktop.
class ConvertsNumberAlphaID
ALPHAS = %w( a b c d e f
g h i j k l
m n o p q r
s t u v w x
y z 0 1 2 3
4 5 6 7 8 9
A B C D E F
G H I J K L
M N O P Q R
S T U V W X
Y Z )
BASE = ALPHAS.length
def self.from_number(number)
out = ""
shifted_number = Math.log(number, BASE).floor
while shifted_number >= 0
powered_base = (BASE ** shifted_number)
index = (number / powered_base).floor % BASE
out = ALPHAS[index] + out
number = number - (index * powered_base)
shifted_number -= 1
end
out
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment