Skip to main content

How to Generate UUID in Ruby

Using SecureRandom.uuid

How to Generate UUID in Ruby. Call SecureRandom.uuid to generate a Ruby UUID in one line. Looking for random IDs on iOS, macOS, watchOS and tvOS? The swift uuid page has copy-paste code.

Generate UUID in Ruby

SecureRandom.uuid
550e8400-e29b-41d4-a716-446655440000

Ruby ships everything you need to generate a UUID without installing a single gem: the SecureRandom module's uuid method hands back a random, cryptographically secure UUID string in one line. There's no dedicated UUID class in Ruby's standard library, so that string is exactly what you get — ready to drop straight into session tokens, API keys, or a database primary key. Below you'll find copy-paste ready code for generating a UUID in Ruby, validating that a string is a properly formatted UUID, and the third-party options available when SecureRandom's v4-only output isn't enough.

How to Generate a UUID in Ruby

The simplest way to generate a UUID in Ruby is SecureRandom.uuid, built into the standard library's securerandom module — no gem install required. It returns a random, RFC 4122-compliant version 4 UUID as a plain String. For shell scripts on Linux, uuidgen usage, case normalisation and a regex check are all shown in the bash uuid guide for beginners.

main.rb
require 'securerandom'

my_uuid = SecureRandom.uuid
puts my_uuid
# Output: 550e8400-e29b-41d4-a716-446655440000

Explanation

Convert a String to a UUID in Ruby

Ruby's standard library treats UUIDs as plain strings, so there's no dedicated type to convert a string into — what you actually need is to confirm a string is a well-formed UUID before you trust it. A regular expression handles that in one line. Developers who want compile-time safety instead of raw strings can start with the rust uuid and its copy-paste examples.

main.rb
UUID_REGEX = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i

def valid_uuid?(str)
  UUID_REGEX.match?(str)
end

valid_uuid?("550e8400-e29b-41d4-a716-446655440000") # true
valid_uuid?("not-a-uuid")                            # false

Explanation

Other Options for Generating UUIDs in Ruby

SecureRandom.uuid only generates version 4 (random) UUIDs. For version 1, 3, or 5 identifiers, or a dedicated UUID object type instead of a plain string, reach for the third-party uuid gem.

Gemfile
gem 'uuid'
main.rb
require 'uuid'

# Random / timestamp-based UUID
uuid = UUID.generate
# UUID.generate also supports namespace-based (v3/v5-style) identifiers

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!