Ruby has a cool method on the String object that allows you to make quick keys easily. The succ method recognizes the pattern you have created and returns the successor (the next in the pattern).
>> s='a99y';3.times do puts s.succ! end
a99z
b00a
b00b
=> 3
This comes in handy when trying to make short “keys” to reference. For example, TinyURL generates a small key to reference an entire URL which is saved in a database (presumably).
This is great, but what happens when we run out of 4 character successors?
>> s='z99y';3.times do puts s.succ! end
z99z
aa00a
aa00b
=> 3
>> s='99y';3.times do puts s.succ! end
99z
100a
100b
=> 3
Ruby has it all figured out. It prepends an alpha or numeric depending on which character was found first in the pattern and it never stops.
>> s='@a1';7019.times do |i| puts "#{s.succ!} ===> #{i}" end
.....
@zz8 ===> 7016
@zz9 ===> 7017
@aaa0 ===> 7018
=> 7019
Note: If you are using this with a database, it is important that you make sure the keys are unique.





