struct

CUID

Inherits Struct / Value / Object

Adds JSON support to CUID.

NOTE: require "cuid/json" is required to use this feature.

require "cuid"
require "cuid/json"

class Example
  JSON.mapping id: CUID
end

example = Example.from_json(%({"id": "ck8vrqmp20000df63z8lsaahr"}))

cuid = CUID.random
cuid.to_json # => "\"ck8vrqmp20000df63z8lsaahr\""

Constants

BASE = 36_u64
BLOCK_SIZE = 4_u64
DISCRETE_VALUES = BASE ** BLOCK_SIZE
RAND_MAX = (BASE ** RAND_SIZE) - 1
RAND_MIN = BASE ** (RAND_SIZE - 1)
RAND_SIZE = BLOCK_SIZE * 2
VERSION = "1.1.0"

Constructors

new(bytes : StaticArray(UInt8, 25))

Generates a CUID from bytes

Source
new(slice : Slice(UInt8))

Generates a CUID from 16-bytes slice.

Source
new(string : String)

Converts a String into a CUID.

Source
new(pull : JSON::PullParser)
Source

Class methods

random

Generates a random CUID

Source

Instance methods

inspect(io : IO) : Nil

Convert to String in a literal format.

Source
to_json(json : JSON::Builder)
Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source