Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guid generator in ruby [duplicate]

Tags:

guid

ruby

I have a problem that is really easily solved with GUIDs.

In particular, for a password reset workflow, I would like to send a GUID token to a user's email and have them reset their password using the token. Since GUIDs are unique, this is pretty secure and saves me emailing people passwords, which is risky.

I noticed there is one uuid gem @ rubyforge but it looks quite old, and it writes stuff to the file system.

Does anyone know of any other gems that can create a globally unique identifier?

I know I can just fall back to:

(0..16).to_a.map{ |a| rand(16).to_s(16) }.join 

But it does not really seem like a proper GUID ...

like image 407
Sam Saffron Avatar asked Jul 13 '09 03:07

Sam Saffron


1 Answers

As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid function.

For example:

require 'securerandom'
SecureRandom.uuid # => "96b0a57c-d9ae-453f-b56f-3b154eb10cda"
like image 159
J _ Avatar answered Sep 23 '22 08:09

J _