UUID in Ruby

Ruby UUID Generator

Ruby's answer is one method from the standard library: SecureRandom.uuid, no gem involved.

Standard librarySecureRandom.uuiduuid_v7

Generated locally with the Web Crypto API — nothing leaves your browser.

Generating UUIDs in Ruby

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

How Ruby UUID Generator Compares

SecureRandom.uuidv4, standard library, CSPRNG
SecureRandom.uuid_v7v7, time-ordered — newer securerandom
SecureRandom.hex(16)32 random hex characters — not a UUID, no version bits

Code Examples

Ruby
require "securerandom"

SecureRandom.uuid                # v4
# => "b8e2f1c0-3a7d-4e11-9c2d-1f0a6b7c8d9e"

SecureRandom.uuid_v7             # v7, time-ordered

# A batch for fixtures
Array.new(5) { SecureRandom.uuid }
ActiveRecord migration
create_table :orders, id: :uuid, default: "gen_random_uuid()" do |t|
  t.string :reference
  t.timestamps
end

# or, generating in Ruby:
class Order < ApplicationRecord
  before_create { self.id ||= SecureRandom.uuid }
end

Frequently Asked Questions

Do I need a gem to generate UUIDs in Ruby?
No — SecureRandom.uuid is in the standard library. A gem is only worth it if you also want parsing, validation or the other versions.
How do I get a UUID v7 in Ruby?
SecureRandom.uuid_v7, available in newer releases of the securerandom gem. On an older Ruby, use the uuid7 gem or generate it in PostgreSQL.
What column type should store a UUID in Rails?
On PostgreSQL, id: :uuid (16 bytes). On MySQL, binary(16). A varchar(36) works but wastes more than twice the space per row.

Related tools