Crystal Examples

CrystalOct 23, 2026

Aligned plain-text terminal table renderer

Renders headers and rows as a column-aligned plain-text table with separator lines for terminal-friendly output.

formattingtablecli
000
CrystalOct 23, 2026

Base64 encode and decode with error handling

Encodes text as strict Base64 and decodes tokens back, converting malformed input into a clear argument error.

decodingencodingbase64
000
CrystalOct 23, 2026

Field-level input validation error map

Validates text fields and returns a hash of per-field error messages instead of failing fast on the first problem.

inputerrorsvalidation
000
CrystalOct 23, 2026

Build and parse URL query params with URI::Params

Builds a percent-encoded query string and parses it back through URI::Params for safe key lookups.

query-stringurluri
000
CrystalOct 23, 2026

JSON decode with line and column error details

Decodes JSON into a struct and rewrites parse or shape failures as readable errors with line and column details.

serializationerrorsjson
000
CrystalOct 23, 2026

Bounded fiber worker pool with indexed results

Runs items on a fixed number of worker fibers and returns indexed results tagged with either a value or an error.

concurrencyworker-poolfibers
000
CrystalOct 23, 2026

Environment config with defaults and range checks

Reads environment variables with defaults, integer parsing and minimum-value checks, raising clear config errors.

configenvvalidation
000
CrystalOct 23, 2026

HTTP JSON GET with connect and read timeouts

Sends an HTTP GET with connect and read timeouts, verifies the status code and parses the JSON response body.

httpjsontimeout
000
CrystalOct 23, 2026

Time-based token bucket rate limiter

Allows short bursts up to the bucket size while enforcing an average request rate by refilling tokens over time.

token-bucketrate-limittime
000
CrystalOct 23, 2026

Bounded LRU cache backed by a Hash

Keeps recently used entries in a size-bounded cache and evicts the least recently used entry whenever it is full.

lrucachehash
000
CrystalOct 23, 2026

Chunk an Enumerable into fixed-size batches

Splits any Enumerable into arrays of at most N items, keeps a short final batch and rejects non-positive sizes.

chunkenumerablebatch
000
CrystalOct 23, 2026

Stable multi-key sort with nil values last

Sorts records by several keys, places nil values last and breaks ties with the original index so the order is stable.

sortrecordscomparator
000
CrystalOct 23, 2026

Stream file lines with File.each_line

Yields trimmed, non-blank, non-comment lines from a text file and turns a missing file into a clear argument error.

streamingfileio
000
CrystalOct 23, 2026

Deep clone and merge JSON::Any trees

Recursively clones and merges JSON::Any values, so nested hashes and arrays are copied instead of shared between callers.

mergejsondeep-clone
000
CrystalOct 23, 2026

Retry with capped jittered backoff in Crystal

Retries a block with capped exponential backoff and full jitter, re-raising the last error once attempts run out.

backoffretryjitter
000