Generate UUID v4 and v7 in Ruby
Copy-paste Ruby code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in Ruby
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
require 'securerandom'
id = SecureRandom.uuid # v4 — uses /dev/urandom
puts id
UUID v7 in Ruby
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
# Use the 'uuid7' gem:
# gem install uuid7
require 'uuid7'
id = UUID7.generate
puts id
Library support
Ruby's standard library doesn't ship v7 yet (as of Ruby 3.4). Use the `uuid7` gem — small, no dependencies, just the v7 algorithm.
Notes
`SecureRandom.uuid` is the right call for v4 in any modern Ruby. Rails uses it under the hood for `has_secure_token` and similar primitives.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.