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

You can use the SecureRandom library in Ruby to generate a secure password with a custom length and only lowercase letters.

Here's an example:

main.rb
require 'securerandom'

def generate_password(length)
  chars = ('a'..'z').to_a # array of lowercase letters
  Array.new(length) { chars.sample }.join # join random chars into a string
end

# generate a password with a length of 8
password = generate_password(8)

puts password # => "ofpemqhi"
294 chars
12 lines

In the generate_password method, we create an array of lowercase letters using the range 'a'..'z'. We then create an array of length length (specified as an argument to the method) using Array.new, where each element in the array is a randomly selected lowercase letter using sample. Finally, we join the array into a string using join.

Note that this method only generates a password with lowercase letters. For a more secure password, you may want to include uppercase letters, numbers, and special characters.

gistlibby LogSnag