tags:

views:

228

answers:

1

While reading through another question here, on creating a URL shortening service, it was suggested to take the ID of the row, and then convert it to base 62 (0 to 9, a to z, and A to Z). Is there a standard function to accomplish this in Ruby?

Edit: Not sure if I was clear enough.

Something like this: 123456.radix(62)

should give the base 62 of 123456.

I'm thinking of writing one, if it isn't already there.

+3  A: 

The to_s method in Ruby integer classes could take an optional parameter specifying the base, something like:

123456.to_s(16)

However, it only takes accepts values from 2 to 36 as the radix.

This snippet might be a good starting place if you want to write your own encoder. There's also a base62 Ruby Gem that adds methods for encoding and decoding in base62.

Firas Assaad