URL Shortening in Ruby on Rails

I was creating a project the other day that used anonymous pages for users and I wanted to make it a little less "guessable" but more human friendly than a SHA1 hash, so I decided to make a small home-brew URL shortener similar in format to Tiny URL.

The neat thing about ruby is that when you turn a Fixnum to a String with tos you can specify the base, and when you turn a String back to a Fixnum you can also specify a base in toi. This lends itself perfectly to turning (large) IDs into a shortened URL thats easier to remember.

In the first gist you can see how this would work with a model to make simple url shortening in rails -- or, you can use randomized values to generate more user friendly "unique" tokens (as opposed to using SHA1 hashes for user confirmation/password reset pages).