W3cubDocs

/Crystal

struct UUID

Overview

Represents a UUID (Universally Unique IDentifier).

Defined in:

uuid.cr
uuid/json.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

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

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

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

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

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

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

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

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.

def self.new(pull : JSON::PullParser)Source

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)

Class Method Detail

def self.emptySource

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

Generates RFC 4122 v4 UUID.

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

Instance Method Detail

def ==(other : UUID)Source

Returns true if other UUID represents the same UUID, false otherwise.

def bytes : StaticArray(UInt8, 16)Source

Returns the binary representation of the UUID.

def hexstringSource

def inspect(io : IO) : NilSource

Convert to String in literal format.

def to_json(json : JSON::Builder)Source

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\""

def to_s(io : IO) : NilSource

Description copied from struct Struct

Same as #inspect(io).

def to_unsafeSource

Returns unsafe pointer to 16-bytes.

def urnSource

def v1!Source

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

def v1?Source

Returns true if UUID is a V1, false otherwise.

def v2!Source

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

def v2?Source

Returns true if UUID is a V2, false otherwise.

def v3!Source

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

def v3?Source

Returns true if UUID is a V3, false otherwise.

def v4!Source

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

def v4?Source

Returns true if UUID is a V4, false otherwise.

def v5!Source

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

def v5?Source

Returns true if UUID is a V5, false otherwise.

def variantSource

Returns UUID variant.

def versionSource

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

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/UUID.html