Created
December 14, 2008 14:17
-
-
Save mikker/35692 to your computer and use it in GitHub Desktop.
Creating random code strings with Array#inject
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
# (to learn inject I am...) | |
# Using inject to create random strings | |
def code(size) | |
chars = ("a".."z").to_a+(0..9).to_a | |
Array.new(size).inject("") { |str, n| str += chars[rand(chars.size)].to_s } | |
end | |
50.times { |i| puts code(i) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment