unique id custom creator - help

14 views
Skip to first unread message

oliver torres

unread,
Jul 27, 2010, 6:52:36 PM7/27/10
to Ruby on Rails: Talk
I have a simple blog app that lets me create an entry with title and a
body... real simple.
In addition to the id that the database creates by default per each
entry I would love to associate an unique id that uses
0123456789 and the 26 letters of the English alphabet.

So that means I have 36^7 unique ids right? assuming I want to keep
each unique combination no bigger than 7 digits, Why? This is
hypothetical.
Can anyone help point me into the right direction?
the intended result would be 00001, 00002, 00003 etc etc.

I was thinking to using a foor loop but a friend said that was a bad
idea.
A newbie here, I guess i dont have to point that out, anyone that can
help me will be great.
Thanks
Oliver.


Jeffrey L. Taylor

unread,
Jul 28, 2010, 9:54:25 AM7/28/10
to Ruby on Rails: Talk

irb(main):006:0> 50.to_s(36)
=> "1e"

50 base 36 = 1*36 + 14

HTH,
Jeffrey

Marnen Laibow-Koser

unread,
Jul 28, 2010, 2:06:08 PM7/28/10
to rubyonra...@googlegroups.com
Jeffrey L. Taylor wrote:

> Quoting oliver torres <senor...@gmail.com>:
>> the intended result would be 00001, 00002, 00003 etc etc.
>>
>> I was thinking to using a foor loop but a friend said that was a bad
>> idea.
>> A newbie here, I guess i dont have to point that out, anyone that can
>> help me will be great.
>> Thanks
>
> irb(main):006:0> 50.to_s(36)
> => "1e"

That works really well. If you'd like something more sophisticated, you
could implement Doug Crockford's base-32 algorithm described at
http://crockford.com/wrmg/base32.html . I've got some code for that
algorithm at
http://groups.google.com/group/techvalleyrb/msg/ac3ce4ae6620268a that I
probably should put into a gem...

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages