Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created June 16, 2024 22:54
Show Gist options
  • Save havenwood/3963ae150acf640385863838a2516426 to your computer and use it in GitHub Desktop.
Save havenwood/3963ae150acf640385863838a2516426 to your computer and use it in GitHub Desktop.
A simple Nano ID implementation in Ruby
# frozen_string_literal: true
require 'securerandom'
module NanoID
ALPHABET = [*'A'..'Z', *'a'..'z', *'0'..'9', '_', '-'].freeze
module_function
def nano_id(alphabet: ALPHABET, size: 21)
SecureRandom.send(:choose, alphabet, size)
end
end
include NanoID
puts nano_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment