Forked from KamilLelonek/random_string_generator.rb
Created
September 2, 2019 20:09
-
-
Save lym/1d55a0af5ff9d70f16efae117f0215b5 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment