diff --git a/README.md b/README.md index 3294ad5..2aef3fb 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,14 @@ Randomstring.generate Randomstring.generate(7) # >> "xqm5wXX" + +Randomstring.generate_alphanumeric_lower +# >> "4vhp56ijj5lwfd73df1mw07b9llfw783" + +Randomstring.generate_custom(8, ['A'..'C']) +# >> "BBABAAAB" ``` ## LICENSE -ruby-randomstring is licensed under the MIT license. \ No newline at end of file +ruby-randomstring is licensed under the MIT license. diff --git a/lib/randomstring.rb b/lib/randomstring.rb index c6a0496..3d0a674 100644 --- a/lib/randomstring.rb +++ b/lib/randomstring.rb @@ -1,8 +1,16 @@ class Randomstring + def self.generate_custom(length = 32, range) + characters = range.map{ |i| i.to_a }.flatten + (0...length).map{ characters[rand(characters.length)] }.join + end + def self.generate(length = 32) - characters = [('0'..'9'), ('a'..'z'), ('A'..'Z')] - characters = characters.map{ |i| i.to_a }.flatten - string = (0...length).map{ characters[rand(characters.length)] }.join - string + range = [('0'..'9'), ('a'..'z'), ('A'..'Z')] + generate_custom(length, range) + end + + def self.generate_alphanumeric_lower(length = 32) + range = [('0'..'9'), ('a'..'z')] + generate_custom(length, range) end -end \ No newline at end of file +end