Skip to content

struct UUID
inherits Struct

Represents a UUID (Universally Unique IDentifier).

Class methods

.empty

View source

.from_json_object_key?(key : String)

Deserializes the given JSON key into a UUID.

Note

require "uuid/json" is required to opt-in to this feature.

View source

.random(random = Random::Secure, variant = Variant::RFC4122, version = Version::V4)

Generates RFC 4122 v4 UUID.

It is strongly recommended to use a cryptographically random source for random, such as Random::Secure.

View source

.new(bytes : StaticArray(UInt8, 16), variant : UUID::Variant? = nil, version : UUID::Version? = nil)

Generates UUID from bytes, applying version and variant to the UUID if present.

View source

.new(slice : Slice(UInt8), variant = nil, version = nil)

Creates UUID from 16-bytes slice. Raises if slice isn't 16 bytes long. See #initialize for variant and version.

View source

.new(uuid : UUID, variant = nil, version = nil)

Creates another UUID which is a copy of uuid, but allows overriding variant or version.

View source

.new(value : String, variant = nil, version = nil)

Creates new UUID by decoding value string from hyphenated (ie. ba714f86-cac6-42c7-8956-bcf5105e1b81), hexstring (ie. 89370a4ab66440c8add39e06f2bb6af6) or URN (ie. urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20) format.

View source

.new(pull : JSON::PullParser)

Creates UUID from JSON using JSON::PullParser.

Note

require "uuid/json" is required to opt-in to this feature.

require "json"
require "uuid"
require "uuid/json"

class Example
  include JSON::Serializable

  property id : UUID
end

example = Example.from_json(%({"id": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
View source

Methods

#==(other : self)

#bytes : StaticArray(UInt8, 16)

Returns the binary representation of the UUID.

View source

#hash(hasher)

#hexstring

View source

#inspect(io : IO) : Nil

Convert to String in literal format.

View source

#to_json(json : JSON::Builder)

Returns UUID as JSON value.

Note

require "uuid/json" is required to opt-in to this feature.

uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
uuid.to_json # => "\"87b3042b-9b9a-41b7-8b15-a93d3f17025e\""
View source

#to_s(io : IO) : Nil

Same as #inspect(io).

View source

#to_unsafe

Returns unsafe pointer to 16-bytes.

View source

#v1!

Returns true if UUID is a V1, raises Error otherwise.

View source

#v1?

Returns true if UUID is a V1, false otherwise.

View source

#v2!

Returns true if UUID is a V2, raises Error otherwise.

View source

#v2?

Returns true if UUID is a V2, false otherwise.

View source

#v3!

Returns true if UUID is a V3, raises Error otherwise.

View source

#v3?

Returns true if UUID is a V3, false otherwise.

View source

#v4!

Returns true if UUID is a V4, raises Error otherwise.

View source

#v4?

Returns true if UUID is a V4, false otherwise.

View source

#v5!

Returns true if UUID is a V5, raises Error otherwise.

View source

#v5?

Returns true if UUID is a V5, false otherwise.

View source

#variant

Returns UUID variant.

View source

#version

Returns version based on RFC4122 format. See also #variant.

View source