Created
May 27, 2017 13:17
-
-
Save Cyan101/ea91638e8f0a9d76bd6fac764c724d42 to your computer and use it in GitHub Desktop.
Creates a 8-character unique ID based off the current time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_id | |
DateTime.now.strftime('%Q').to_i.to_s52 | |
end | |
class Numeric | |
Alpha52 = ('a'..'z').to_a + ('A'..'Z').to_a | |
def to_s52 | |
return '' if self < 1 | |
s = '' | |
q = self | |
loop do | |
q, r = (q - 1).divmod(52) | |
s.prepend(Alpha52[r]) | |
break if q.zero? | |
end | |
s | |
end | |
end | |
# puts unique_id | |
#=> awHwanat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment