alphadecimal (base 36) hashes in ruby

Here’s a little trick I discovered about a year ago. The Integer#to_s method takes an argument to change the base (it defaults to base 10 of course). The highest base you can use is 36, which is [a-z0-9]. There are many names for base 36, including the proper sexatrigesimal, the common hextridecimal, and the more modern alphadecimal.

Combined with the String#hash and Time#hash methods, you can get sweet little TinyUrl style hashes.

check it:

# timestamp
>> DateTime.now.hash.abs.to_s(36)
=> "5ynfka" 

# invite code
>> "joe@momma.org".hash.to_s(36)
=> "emhs98" 

Not sure of the ultimate-in-the-entire-universe uniqueness of this type of hash, which I believe is based on some simple XORs… anyone care to comment?

actionmailer ActionView ActiveRecord ajax alphadecimal audio autotest BDD blocks capistrano ssh ruby controller css dashboard widget delegate dog puppy naming name DRM email obfuscation exceptions factories-and-workers factory pattern filemerge find and replace finder fink fixtures fun gem git google maps helper helpers imagemagick Intertrust javascript logo math meetup model openssl OS X patch Pioneer Electronics plugin polymorphism prototype.js rails rails gotcha railsconf rake rmagick RSA encryption ruby script shoulda subversion SyncTV TDD testing textmate tricks unique hashes unix shell validation view yaml zebra stripes