UUID
UUID module: RFC 4122 UUID generation and manipulation.
The UUID type wraps a canonical string representation: "xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx" where M is the version digit and N is the variant digit (8, 9, a, or b).
Supported versions: v4 — randomly generated (RFC 4122 §4.4) v5 — name-based using SHA-1 (RFC 4122 §4.3) NOTE: v5 uses the first 16 bytes of a SHA-1 hash with version/ variant bits set; the resulting UUID is RFC-compliant.
The nil UUID ("00000000-0000-0000-0000-000000000000") is the all-zeros UUID. version/1 returns 0 for the nil UUID.
Types
Functions
Generate a random UUID v4.
UUID.v4() -- e.g. UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479")
Uses the runtime's CSPRNG. Version bits (4) and variant bits (RFC 4122) are set correctly.
Parse a UUID string, returning Ok(UUID) or Err(:invalid).
Accepts both upper and lower case hex digits; validates the xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format (36 characters, 4 dashes).
UUID.parse("f47ac10b-58cc-4372-a567-0e02b2c3d479") -- Ok(UUID(...)) UUID.parse("not-a-uuid") -- Err(:invalid)
Convert a UUID to its canonical lowercase string representation.
UUID.to_string(UUID.nil()) -- "00000000-0000-0000-0000-000000000000"
The nil UUID: all 128 bits are zero.
UUID.to_string(UUID.nil()) -- "00000000-0000-0000-0000-000000000000"
Generate a name-based UUID v5 (SHA-1, RFC 4122 §4.3).
let dns = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") UUID.v5(dns, "www.example.com") -- deterministic UUID
The namespace must be a valid UUID. The name is an arbitrary string. The result is deterministic: the same namespace + name always produce the same UUID.
Return the version number of a UUID (1–5), or 0 for unrecognised/nil.
UUID.version(UUID.v4()) -- 4 UUID.version(UUID.nil()) -- 0
Return true if the string is a valid UUID in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx form.
UUID.is_valid("f47ac10b-58cc-4372-a567-0e02b2c3d479") -- true UUID.is_valid("00000000-0000-0000-0000-000000000000") -- true UUID.is_valid("not-a-uuid") -- false