Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Last active September 2, 2019 20:09
Show Gist options
  • Save KamilLelonek/4d9b4339dfa07c34830e to your computer and use it in GitHub Desktop.
Save KamilLelonek/4d9b4339dfa07c34830e 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
@lym
Copy link

lym commented Sep 2, 2019

Nice and easy. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment