Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
ruby-randomstring is licensed under the MIT license.
18 changes: 13 additions & 5 deletions lib/randomstring.rb
Original file line number Diff line number Diff line change
@@ -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
end