justinp.io projects about contact

ULIDTools is designed to provide a ruby implementation of ULID that takes advantage of their 128-bit compatibility with UUIDs. It's a little heavier weight than the main ruby implementation, and has methods to more easily create and manipulate ULIDs. You can view the project on my GitHub

The ULID is stored internally as a string of bytes. The class has a few methods on it to make creating, manipulating, and storing them easier.

u = ULIDTools.generate 
u.raw # => "\x01iANo%pB\xCC\v\xD4N\xBB\xF4#\\"
u.to_s # => "01D50MWVS5E11CR2YM9TXZ88TW"
u.to_uuid # => "0169414e-6f25-7042-cc0b-d44ebbf4235c"

# Sorting also works!

u1 = ULIDTools.generate
sleep 5
u2 = ULIDTools.generate

u2 < u1 # => true

# You can also create ULIDs as if it was a specific time.
# Useful for backfilling indexes

u3 = ULIDTools.at_time(Time.at(728121600))

u3.to_s # => "00N63PVT00M6VD4GJJ6D4G6TMB"

# And ULIDs know their timestamps as well

u3.time # => 1993-01-27 00:00:00 -0800

You can also use a Plain-old-Ruby object in Rails to serialize ULIDs and UUIDs like so:

# Somewhere
class ULIDSerializer
  def self.load(uuid)
    return nil if uuid.nil?

  def self.dump(ulid)

# In the Model
class SomeModel < ApplicationRecord
  serialize :ulid, ULIDSerializer

In fact, I use ULIDs on this site as resource locators for inserting images and links! This page's ULID is 01DE6G74KGGRE4SSP3CG4YTHAC