Hello, I need to generate a unique ID, so far I''ve unit tested this under 100000 iterations and it seems to work : now.to_i.to_s + ''-'' + now.usec.to_s + ''-'' + rand(1000).to_s Is there a better way ? Thanks Notes : 1) without usec, few percents of generated values are not uniques 2) the id generated is a string so the presence of ''-'', this is only for a better readibility in case of SQL debugging -- Posted via http://www.ruby-forum.com/.
nuno wrote:> Hello, I need to generate a unique ID, so far I''ve unit tested this > under 100000 iterations and it seems to work : > > now.to_i.to_s + ''-'' + now.usec.to_s + ''-'' + rand(1000).to_s > > Is there a better way ? > > Thanks > > > Notes : > 1) without usec, few percents of generated values are not uniques > 2) the id generated is a string so the presence of ''-'', this is only for > a better readibility in case of SQL debugginguuidgen. It''s quite slow, but you can run it once and append 10,000 sequential integers to the result and now you have 10,000 cached unique values. When you use them up, get another seed-value from uuidgen. -- Posted via http://www.ruby-forum.com/.
Francis Cianfrocca wrote:> nuno wrote: >> Hello, I need to generate a unique ID, so far I''ve unit tested this > > uuidgen. It''s quite slow, but you can run it once and append 10,000 > sequential integers to the result and now you have 10,000 cached unique > values. When you use them up, get another seed-value from uuidgen.But uuidgen seems to be a unix command, I didn''t found anything related with Ruby... Am I wrong ? My app will run under Windows XP, so I can''t use unix like commands (and installing cygwin is not a solution) -- Posted via http://www.ruby-forum.com/.
On 8/8/06, nuno <nomail@novalidatall991.com> wrote:> Hello, I need to generate a unique ID, so far I''ve unit tested this > under 100000 iterations and it seems to work : > > now.to_i.to_s + ''-'' + now.usec.to_s + ''-'' + rand(1000).to_s > > Is there a better way ? >Your id mechanism is vulnerable to time being reset, if you are persisting these, especially if ids across multiple machines must be compatible in some way (stored in a shared database).
nuno wrote:> Francis Cianfrocca wrote: >> nuno wrote: >>> Hello, I need to generate a unique ID, so far I''ve unit tested this >> >> uuidgen. It''s quite slow, but you can run it once and append 10,000 >> sequential integers to the result and now you have 10,000 cached unique >> values. When you use them up, get another seed-value from uuidgen. > > But uuidgen seems to be a unix command, I didn''t found anything related > with Ruby... Am I wrong ? > > My app will run under Windows XP, so I can''t use unix like commands (and > installing cygwin is not a solution)In Ruby: `uuidgen` will work under both unix and windows. -- Posted via http://www.ruby-forum.com/.
Francis Cianfrocca wrote:> > In Ruby: `uuidgen` will work under both unix and windows.Okay !! I thought it was a method name... But what is the meaning of these strange quotes `` ??? -- Posted via http://www.ruby-forum.com/.
nuno wrote:> Francis Cianfrocca wrote: >> >> In Ruby: `uuidgen` will work under both unix and windows. > > Okay !! I thought it was a method name... But what is the meaning of > these strange quotes `` ???Read the Ruby language docs: enclosing a string in backticks causes it to be executed in a shell. You can put arbitrarily complex commands in there, same as you would at a shell prompt. You can also put #{...} as in a Ruby double-quoted string. Your program stops running until the command completes, and in Ruby, you get back a String which contains whatever the command wrote to stdout. uuidgen is present on Windows, definitely, but doesn''t support the -r option you often find on Unix. So just use it without -r. -- Posted via http://www.ruby-forum.com/.
Another possibility would be to ''gem install uuid'' . Read the README of this gem and use it within your rails code. Cheers, Jan On 8/8/06, Francis Cianfrocca <garbagecat10@gmail.com> wrote:> > nuno wrote: > > Francis Cianfrocca wrote: > >> > >> In Ruby: `uuidgen` will work under both unix and windows. > > > > Okay !! I thought it was a method name... But what is the meaning of > > these strange quotes `` ??? > > Read the Ruby language docs: enclosing a string in backticks causes it > to be executed in a shell. You can put arbitrarily complex commands in > there, same as you would at a shell prompt. You can also put #{...} as > in a Ruby double-quoted string. Your program stops running until the > command completes, and in Ruby, you get back a String which contains > whatever the command wrote to stdout. > > uuidgen is present on Windows, definitely, but doesn''t support the -r > option you often find on Unix. So just use it without -r. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060808/e1cc79ff/attachment-0001.html
Francis Cianfrocca wrote:> nuno wrote: >> Francis Cianfrocca wrote: >>> >>> In Ruby: `uuidgen` will work under both unix and windows. >> >> Okay !! I thought it was a method name... But what is the meaning of >> these strange quotes `` ??? > > Read the Ruby language docs: enclosing a string in backticks causes it > to be executed in a shell. You can put arbitrarily complex commands in > there, same as you would at a shell prompt. You can also put #{...} as > in a Ruby double-quoted string. Your program stops running until the > command completes, and in Ruby, you get back a String which contains > whatever the command wrote to stdout. >Thanks for the hints ! -- Posted via http://www.ruby-forum.com/.