### Numerical ID Encoding and Sorting Source: https://github.com/sudhirj/shortuuid.rb/blob/master/README.md Illustrates encoding numerical IDs (like timestamps) into shorter strings using the default alphabet. It also demonstrates that the default alphabet maintains lexicographical order for same-length encoded strings, and how to use `rjust` for proper comparison. ```ruby require 'shortuuid' t1 = Time.now.to_i puts "Timestamp 1: #{t1}" encoded_t1 = ShortUUID.encode(t1) puts "Encoded Timestamp 1: #{encoded_t1}" t2 = Time.now.to_i puts "Timestamp 2: #{t2}" encoded_t2 = ShortUUID.encode(t2) puts "Encoded Timestamp 2: #{encoded_t2}" puts "Timestamp 1 < Timestamp 2: #{t1 < t2}" puts "Encoded Timestamp 1 < Encoded Timestamp 2: #{encoded_t1 < encoded_t2}" a = 42 b = rand(9999999999999999) puts "Number a: #{a}" puts "Number b: #{b}" puts "a < b: #{a < b}" puts "Encoded a < Encoded b: #{ShortUUID.encode(a) < ShortUUID.encode(b)}" puts "Encoded a (padded) < Encoded b (padded): #{ShortUUID.encode(a).rjust(10, '0') < ShortUUID.encode(b).rjust(10, '0')}" puts "Encoded numbers: #{[a, b].map { |x| ShortUUID.encode(x) }}" puts "Encoded numbers (padded): #{[a, b].map { |x| ShortUUID.encode(x).rjust(10, '0') }}" ``` -------------------------------- ### UUID Shortening and Expansion Source: https://github.com/sudhirj/shortuuid.rb/blob/master/README.md Demonstrates how to shorten a standard UUID into a shorter string using the default base62 alphabet and then expand it back to the original UUID. It also shows how to use a custom alphabet for shortening and expansion. ```ruby require 'securerandom' require 'shortuuid' id = SecureRandom.uuid puts "Original UUID: #{id}" short_id = ShortUUID.shorten id puts "Shortened ID: #{short_id}" expanded_id = ShortUUID.expand short_id puts "Expanded ID: #{expanded_id}" vowel_id = ShortUUID.shorten id, "AEIOU".chars puts "Shortened ID with Vowel Alphabet: #{vowel_id}" expanded_vowel_id = ShortUUID.expand vowel_id, "AEIOU".chars puts "Expanded ID from Vowel Alphabet: #{expanded_vowel_id}" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.