Skip to main content

How to Generate a UUID in Ruby with SecureRandom.uuid

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

SecureRandom.uuid returns a new random UUID as a string, such as 5b558bcd-a815-462d-afb3-a2616f697fd9. It comes with Ruby, so require 'securerandom' is all you need, and SecureRandom.uuid_v7 makes a time-ordered one.

Syntax​

Syntax
require 'securerandom'

<id> = SecureRandom.uuid
PartWhat it does
require 'securerandom'Loads SecureRandom, which is part of Ruby's standard library.
SecureRandom.uuidReturns a new random version 4 UUID as a string.
<id>The variable that holds it.

Ruby's documentation for uuid, which SecureRandom provides: "Generate a random v4 UUID (Universally Unique IDentifier)."

The Example​

The example makes a UUID and prints it and its length, then makes a second UUID and a version 7 UUID.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which requires securerandom, stores SecureRandom.uuid in id, prints it and its length, then prints a second SecureRandom.uuid and a SecureRandom.uuid_v7.

main.rb
require 'securerandom'

id = SecureRandom.uuid
puts id
puts id.length
puts SecureRandom.uuid
puts SecureRandom.uuid_v7

Step 2: Run It​

Terminal
ruby main.rb
Output
5b558bcd-a815-462d-afb3-a2616f697fd9
36
748001a9-cada-437f-b87b-dd52a6266016
01a22d7c-b273-70ad-8e64-517b22e35717

A zsh terminal where ruby main.rb prints a UUID, the length 36, a second random UUID and a version 7 UUID starting with 01a22d7c.

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.

What Happened​

  • SecureRandom.uuid returned a string, ready to print or store, 36 characters long.
  • The two random UUIDs are unrelated. Both have a 4 at the start of their third group, the version digit of a random UUID.
  • uuid_v7 has a 7 there, and starts with the current time, so newer ones sort after older ones.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out require 'securerandom'uninitialized constant SecureRandom in a plain scriptRequire it first
Using rand for IDs that must not be guessedRuby's documentation says its algorithm "is not for cryptographical use, you must use SecureRandom for security purpose"Use SecureRandom
Expecting uuid to sort by timeVersion 4 UUIDs are randomUse uuid_v7 when order matters

Troubleshooting​

SymptomLikely causeFix
uninitialized constant SecureRandom (NameError)securerandom isn't loadedAdd require 'securerandom'
Many IDs start with the same charactersThey come from uuid_v7, whose first part is the current timeThat's how version 7 works; use uuid for fully random IDs

FAQ​

Do I need a gem to make UUIDs in Ruby?​

No. SecureRandom is part of Ruby, so a plain require 'securerandom' works without Bundler or a gem.

Is SecureRandom.uuid random enough for IDs that must not be guessed?​

Yes. Ruby's documentation describes SecureRandom as "an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc." Don't use a UUID as a password or a key, though: anyone who sees it can copy it.

Conclusion​

SecureRandom.uuid gives you a random version 4 UUID as a string, and SecureRandom.uuid_v7 a time-ordered one. Require securerandom first.

Additional Resources​

  • Random::Formatter, whose uuid and uuid_v7 methods SecureRandom provides, in the Ruby documentation.