Password Generator in Ruby

This is the simple password generator that i wrote in Ruby. Firstly i am creating array of characters, numbers and symbols and then i am putting all those arrays in sigle place called secret(which is array). While I am joining all these arrays together, I am calling “Shuffle” method on them so that every time secret array have elements in different order. Once my secrete array is ready then I am generating password from secrete array. First of all i am using shuffle method on secrete array and then i am taking first 15 elements from secrete array using “take” method. I could have also used range(0..14) but I prefer using “take” method and finally I am joining all those 15 elements together using “join” method. Its very simple and useful method. This method will generate random password when ever its called.

def password_generator
  secrete = []
  char_array = ("a".."z").to_a
  num_array  = (0..9).to_a
  symbol_array = %w[! @ # $ % ^ & * ( ) _ - + = { [ } ] : ; " ' < , > . ? /]

  secrete.concat(char_array.shuffle)
  secrete.concat(num_array.shuffle)
  secrete.concat(symbol_array.shuffle)

  password = secrete.shuffle.take(15).join
end

Outputs

$j4″kzp8;}.@)hu
qkxu#'{6&i,>r3?
l5,v%a};t’31$?w
?>w4&c@o(}){+#2
^h[w<cg’x3y*6a$

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s