# Base62 encoding in Ruby # Useful for creating tiny url slugs from numeric ids # # Example: # Base62.encode 1_000_000 # => "4c92" module Base62 CHARS = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a # Adapted from http://refactormycode.com/codes/125-base-62-encoding def self.encode(i) return '0' if i == 0 s = '' while i > 0 s << CHARS[i.modulo(62)] i /= 62 end s.reverse! s end end