Last active
September 2, 2019 20:09
-
-
Save KamilLelonek/4d9b4339dfa07c34830e to your computer and use it in GitHub Desktop.
Ruby random string generator with particular length
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
class RandomStringGenerator | |
def without_numbers(length) | |
random_string(length, alphabet) | |
end | |
def with_numbers(length) | |
random_string(length, alphabet + numbers) | |
end | |
private | |
def random_string(length, charset) | |
Array.new(length) { charset.sample }.join | |
end | |
def alphabet | |
@alphabet ||= Array('A'..'Z') + Array('a'..'z') | |
end | |
def numbers | |
@numbers ||= Array(0 .. 9) | |
end | |
end |
Author
KamilLelonek
commented
Mar 19, 2015
Nice and easy. Thanks for sharing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment