Created
November 20, 2011 18:02
-
-
Save DarrenN/1380593 to your computer and use it in GitHub Desktop.
Short unique ID (Ruby)
This file contains hidden or 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
t = DateTime | |
id = t.now.strftime("%Y%m%d%k%M%S%L") # Get current date to the milliseconds | |
id = id.to_i.to_s(36) # will generate somthing like "5i0sp1h4tkc" | |
# Reverse it | |
id.to_i(36) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this
Based on this, I use the follow line, which I think is more precise due to the addition of sprintf.
id = sprintf("%20.10f", Time.now.to_f).delete('.').to_i.to_s(36)