-
-
Save sergii/5542352f353e189b6a47a606e76e7a7e to your computer and use it in GitHub Desktop.
Generate random string in Ruby.
This file contains hidden or 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
def random_string(length = 6) | |
rand(36**length).to_s(36) | |
end |
This file contains hidden or 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
def random_string | |
rand(2**1024).to_s(36).upcase[0..9] | |
end |
This file contains hidden or 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
def random_string(length) | |
chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a | |
(1..length).map { | |
chars[rand(chars.length)] | |
}.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment