Module: Pwm
- Defined in:
- lib/pwm.rb,
lib/pwm/version.rb
Defined Under Namespace
Classes: TooShortException
Constant Summary collapse
- VERSION =
"1.2.1"
Class Method Summary collapse
-
.characters ⇒ Object
Internal: The set of characters from which passwords will be assembled.
-
.password(length = 16) ⇒ Object
Public: Generate a password.
Class Method Details
.characters ⇒ Object
Internal: The set of characters from which passwords will be assembled. This could be overridden if you don’t like the default.
Returns the set of characters as an Array.
13 14 15 16 |
# File 'lib/pwm.rb', line 13 def self.characters (('A'..'Z').to_a + ('a'..'z').to_a + ('2'..'9').to_a) - ['I', 'O', 'l'] end |
.password(length = 16) ⇒ Object
Public: Generate a password.
length - The length of the password. Minimum is 8. Default is 16.
Examples
Pwm.password
# => 'SPdHeZnn9rut4AUz'
Pwm.password(8)
# => 'oUX4fmqr'
Returns the generated password as a String if length >= 8, or raises exception if length < 8.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pwm.rb', line 32 def self.password(length = 16) if length < 8 raise Pwm::TooShortException, "Requested length #{length} is too short." else password = '' until (password.match(/[A-Z]/) && password.match(/[a-z]/) && password.match(/[0-9]/)) password = (0..length - 1).inject('') do |pw, n| pw + characters[rand(characters.length)] end end password end end |