Skip to content

Instantly share code, notes, and snippets.

@samueleaton
Created December 14, 2018 17:27
Show Gist options
  • Save samueleaton/8e520d7e052ad0da794e9cb7fbf9bc4f to your computer and use it in GitHub Desktop.
Save samueleaton/8e520d7e052ad0da794e9cb7fbf9bc4f to your computer and use it in GitHub Desktop.
Because sometime your need a big alphanumeric string in Crystal
def rand_str(length = 16, supplemental_chars = "")
res = [] of Char
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + supplemental_chars
length.times do { |i| res << chars[rand(chars.size)] }
res.join()
end
rand_str 32
#=> xkURIpYnqbRVj893pr23LhhxeukFTu5f
# more characters can be used using the 2nd arg
rand_str 48, "!@#$%^&*"
#=> !JzRCOtU%Xr38c*Un$bS3VZygDIfo4lFK#F@^TZAtd8I6QSG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment