Skip to content

Instantly share code, notes, and snippets.

@lym
Forked from KamilLelonek/random_string_generator.rb
Created September 2, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lym/1d55a0af5ff9d70f16efae117f0215b5 to your computer and use it in GitHub Desktop.
Save lym/1d55a0af5ff9d70f16efae117f0215b5 to your computer and use it in GitHub Desktop.
Ruby random string generator with particular length
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