Skip to content

Instantly share code, notes, and snippets.

@mikker
Created December 14, 2008 14:17
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 mikker/35692 to your computer and use it in GitHub Desktop.
Save mikker/35692 to your computer and use it in GitHub Desktop.
Creating random code strings with Array#inject
# (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