0

Not sure if that's the term I should use for it but what I'm trying to do is add len amount of characters to an array, then output that to a .txt. I have the generation done to my satisfaction but I'm not sure how to pack the strings into an array. Right now it just spits out all the strings into the console because of the puts statement, just to make sure it works.

#Password list generator by Nightc||ed, ©2015
norm = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
caps = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
puts "How many passwords would you like to generate?"
num = gets.to_i
system "cls"
puts "Password length (1-x):"
len = gets.to_i
system "cls"
puts """
Which characters would you like to use?
[1] a-z, 0-9
[2] A-Z, 0-9
[3] a-z, A-Z, 0-9
"""
arr = []
type = gets.chomp
if type == "1"
    arr = [norm,nums]
elsif type == "2"
    arr = [caps,nums]
elsif type == "3"
    arr = [norm,caps,nums]
else
    exit
end
num.times do |pass|
    len.times do |char|
        arr2 = arr.to_a.sample
        char = arr2.to_a.sample
        puts char
    end
end
sleep

3 Answers 3

1

here your code simplified

#Password list generator by Nightc||ed, ©2015
norm = [*"a".."z"]
caps = [*"A".."Z"]
nums = [*0..9]
num, len, type = [
   "How many passwords would you like to generate?",
   "Password length (1-x):",
   "Which characters would you like to use?
    [1] a-z, 0-9
    [2] A-Z, 0-9
    [3] a-z, A-Z, 0-9"].map do |msg|
     puts msg
     gets.to_i
   end

arr = case type
      when 1 then
        norm + nums
      when 2 then
        caps + nums
      when 3 then
        norm + caps + nums
      else
        exit
      end

passwords = num.times.map { arr.sample(len).join }
puts passwords.inspect
sleep
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can simplify your life by replacing the if... and below with the following:

case type
  when "1"
    arr = [norm,nums].flatten
  when "2"
    arr = [caps,nums].flatten
  when "3"
    arr = [norm,caps,nums].flatten
  else
    exit
end
passwd_set = []
num.times { passwd_set << arr.sample(len).join }
p passwd_set

I find case statements easier to read, and more easily extended. Flattening the arrays makes it so sample can directly produce the desired number of characters/symbols, and those can be joined to produce a string which can be appended to your passwd_set array.

Comments

0

You can add to an array using the << method. For example:

arr = []
3.times do |el|
  arr << el
end
arr.inspect #=> [0, 1, 2]

Another option would be the push method:

arr = []
(0..2).each { |el| arr.push(el)}

1 Comment

that helps considerably but I need to export to num amount of arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.