A handy ULID
library
The ULID
spec is defined on https://github.com/ulid/spec.
Formal name is Universally Unique Lexicographically Sortable Identifier
.
$ gem install ruby-ulid
The generated ULID
is an object rather than just a string.
It means easily get the timestamps and binary formats.
require 'ulid'
ulid = ULID.generate #=> ULID(2021-04-27 17:27:22.826 UTC: 01F4A5Y1YAQCYAYCTC7GRMJ9AA)
ulid.to_time #=> 2021-04-27 17:27:22.826 UTC
ulid.to_s #=> "01F4A5Y1YAQCYAYCTC7GRMJ9AA"
ulid.octets #=> [1, 121, 20, 95, 7, 202, 187, 60, 175, 51, 76, 60, 49, 73, 37, 74]
You can parse from exists IDs
ulid = ULID.parse('01ARZ3NDEKTSV4RRFFQ69G5FAV') #=> ULID(2016-07-30 23:54:10.259 UTC: 01ARZ3NDEKTSV4RRFFQ69G5FAV)
Providing monotonic generator. It is called as Monotonicity on the spec.
ulids = 10000.times.map do
ULID.generate
end
ulids.sort == ulids #=> false
monotonic_ulids = 10000.times.map do
ULID.monotonic_generate
end
monotonic_ulids.sort == monotonic_ulids #=> true
At first, you should install development dependencies
$ bundle install
Easy to play with the behaviors in REPL.
$ bin/console
If you try to add features, please ensure exist test cases are not broken
$ bundle exec rake test
If you try to improve any performance issue, please check the result of benchmarking before and after.
$ bundle exec ruby benchmark/*
Kenichi Kamiya - @kachick