create a password with a custom length and uppercase letters in ruby

main.rb
def generate_password(length)
  chars = ('a'..'z').to_a + ('A'..'Z').to_a
  password = ''
  length.times { password << chars.sample }
  password
end

# Usage
password_length = 10 # customize this
password = generate_password(password_length).upcase

puts password # example output: "NLKOJIGGXM"
295 chars
13 lines

The generate_password function takes a length argument and generates a password of that length with uppercase letters included. It first defines an array of lowercase letters and uppercase letters, then loops through length times and appends a random character from the array to the password variable. Finally, it returns the generated password in uppercase letters.

gistlibby LogSnag