create a password with numbers in ruby

To create a password with numbers in Ruby, you can use the SecureRandom library to generate random strings.

Here's an example code snippet that generates a random string of length 8, which includes numbers:

main.rb
require 'securerandom'

password = SecureRandom.random_number(10**8).to_s.rjust(8, "0")

puts password
103 chars
6 lines

In this code, SecureRandom.random_number(10**8) generates a random number between 0 and 99,999,999. to_s converts the number to a string, and rjust(8, "0") pads the string with zeros on the left side (if necessary) to ensure that the length of the string is exactly 8 characters.

Feel free to adjust the length of the password by changing the argument for rjust().

gistlibby LogSnag