Crystal Examples
Aligned plain-text terminal table renderer
Renders headers and rows as a column-aligned plain-text table with separator lines for terminal-friendly output.
Base64 encode and decode with error handling
Encodes text as strict Base64 and decodes tokens back, converting malformed input into a clear argument error.
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.
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.
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.
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.
Environment config with defaults and range checks
Reads environment variables with defaults, integer parsing and minimum-value checks, raising clear config errors.
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.
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.
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.
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.
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.
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.
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.
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.