struct UUID
Overview
Represents a UUID (Universally Unique IDentifier).
Defined in:
uuid.cruuid/json.cr
Constructors
- 
        .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. 
- 
        .new(slice : Slice(UInt8), variant = nil, version = nil)
        
          Creates UUID from 16-bytes slice. 
- 
        .new(uuid : UUID, variant = nil, version = nil)
        
          Creates another UUIDwhich is a copy of uuid, but allows overriding variant or version.
- 
        .new(value : String, variant = nil, version = nil)
        
          Creates new UUID by decoding valuestring from hyphenated (ie.
- 
        .new(pull : JSON::PullParser)
        
          Creates UUID from JSON using JSON::PullParser.
Class Method Summary
- .empty
- 
        .random(random = Random::Secure, variant = Variant::RFC4122, version = Version::V4)
        
          Generates RFC 4122 v4 UUID. 
Instance Method Summary
- 
        #==(other : UUID)
        
          Returns trueifotherUUID represents the same UUID,falseotherwise.
- 
        #bytes : StaticArray(UInt8, 16)
        
          Returns the binary representation of the UUID. 
- #hexstring
- 
        #inspect(io : IO) : Nil
        
          Convert to Stringin literal format.
- 
        #to_json(json : JSON::Builder)
        
          Returns UUID as JSON value. 
- 
        #to_s(io : IO) : Nil
        
          Same as #inspect(io).
- 
        #to_unsafe
        
          Returns unsafe pointer to 16-bytes. 
- #urn
- 
        #v1!
        
          Returns trueif UUID is a V1, raisesErrorotherwise.
- 
        #v1?
        
          Returns trueif UUID is a V1,falseotherwise.
- 
        #v2!
        
          Returns trueif UUID is a V2, raisesErrorotherwise.
- 
        #v2?
        
          Returns trueif UUID is a V2,falseotherwise.
- 
        #v3!
        
          Returns trueif UUID is a V3, raisesErrorotherwise.
- 
        #v3?
        
          Returns trueif UUID is a V3,falseotherwise.
- 
        #v4!
        
          Returns trueif UUID is a V4, raisesErrorotherwise.
- 
        #v4?
        
          Returns trueif UUID is a V4,falseotherwise.
- 
        #v5!
        
          Returns trueif UUID is a V5, raisesErrorotherwise.
- 
        #v5?
        
          Returns trueif UUID is a V5,falseotherwise.
- 
        #variant
        
          Returns UUID variant. 
- 
        #version
        
          Returns version based on RFC4122 format. 
Instance methods inherited from struct Struct
  
  
    
      ==(other) : Bool
    ==, 
    
  
    
      hash(hasher)
    hash, 
    
  
    
      inspect(io : IO) : Nil
    inspect, 
    
  
    
      pretty_print(pp) : Nil
    pretty_print, 
    
  
    
      to_s(io : IO) : Nil
    to_s
    
  
    
    
  
    
  Instance methods inherited from struct Value
  
  
    
      ==(other : JSON::Any)==(other : YAML::Any)
==(other) ==, dup dup
Instance methods inherited from class Object
  
  
    
      ! : Bool
    !, 
    
  
    
      !=(other)
    !=, 
    
  
    
      !~(other)
    !~, 
    
  
    
      ==(other)
    ==, 
    
  
    
      ===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(*values : Object) : Bool
in?(collection) : Bool in?, inspect : String
inspect(io : IO) : Nil inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO)
to_json to_json, to_pretty_json(io : IO, indent : String = " ")
to_pretty_json(indent : String = " ") to_pretty_json, to_s : String
to_s(io : IO) : Nil to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as
Class methods inherited from class Object
  
  
    
      from_json(string_or_io, root : String)from_json(string_or_io) from_json, from_yaml(string_or_io : String | IO) from_yaml
Constructor Detail
Generates UUID from bytes, applying version and variant to the UUID if present.
Creates UUID from 16-bytes slice. Raises if slice isn't 16 bytes long. See
#initialize for variant and version.
Creates another UUID which is a copy of uuid, but allows overriding
variant or version.
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.
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
Generates RFC 4122 v4 UUID.
It is strongly recommended to use a cryptographically random source for
random, such as Random::Secure.
Instance Method Detail
Returns true if other UUID represents the same UUID, false otherwise.
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\""