Generating a unique id?

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

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 debugging

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.

Francis C. 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)

On 8/8/06, nuno [email protected] 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).

Francis C. 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 `` ???

nuno wrote:

Francis C. 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.

nuno wrote:

Francis C. 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.

Francis C. wrote:

nuno wrote:

Francis C. 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 !

Another possibility would be to ‘gem install uuid’ .

Read the README of this gem and use it within your rails code.

Cheers,
Jan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs