Skip to content

Instantly share code, notes, and snippets.

@prutya
Created September 30, 2024 10:41
Show Gist options
  • Save prutya/c4b35f72c504e4162f1109a8a61aea9a to your computer and use it in GitHub Desktop.
Save prutya/c4b35f72c504e4162f1109a8a61aea9a to your computer and use it in GitHub Desktop.
Generate consistent UUIDs based on an array of arguments in Ruby (via SHA256 hash)
require 'digest'
require 'securerandom'
def generate_uuid_from_arguments(args)
# Convert the array to a string and then hash it
hash = Digest::SHA256.hexdigest(args.join)
# Use the first 32 characters of the hash to form a UUID
uuid = [
hash[0..7],
hash[8..11],
hash[12..15],
hash[16..19],
hash[20..31]
].join('-')
uuid
end
# Example usage:
args = ['argument1', 'argument2', 'argument3']
puts generate_uuid_from_arguments(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment