Last active
August 14, 2018 14:54
-
-
Save esquinas/407e17a45c41f04843438798557bb5f7 to your computer and use it in GitHub Desktop.
Quick, dirty but typographically resilient UUID codes. Hex digits A, B, C, D, E and F are replaced by letters that cannot be mistaken by any digit or any other letter. The choosen letters are E, J, N, P, V and X.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Takes 3 optional params: an int code_length, a string "separator" and a look-up "table" (hash). | |
def my_uuid_code( | |
code_length = 24, | |
separator: '-', | |
table: { "0" => '0', "1" => '1', "2" => '2', "3" => '3', "4" => '4', "5" => '5', | |
"6" => '6', "7" => '7', "8" => '8', "9" => '9', | |
"A" => 'E', "B" => 'J', "C" => 'N', "D" => 'P', "E" => 'V', "F" => 'X' }) | |
posibilities = 16**code_length | |
return rand(posibilities).to_s(16).upcase.rjust(code_length, '0').chars.map { |digit| | |
table[digit] }.join.scan(/.{1,4}/).join(separator) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment