public
Last active

Creating random code strings with Array#inject

  • Download Gist
inject.rb
Ruby
1 2 3 4 5 6 7 8 9
# (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) }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.