Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/passr/generator.rb

Overview

Extends Ruby String Class to handle special bang methods for password generating

Instance Method Summary collapse

Instance Method Details

#add_capital_letter!(letters) ⇒ String

Add a capital letter to given string

Parameters:

  • letters (Array)

    list of letters, preferably entire lowercase alphabet (a-z)

Returns:

  • (String)

    replaces self with an upper case letter in the place another random character



108
109
110
111
112
# File 'lib/passr/generator.rb', line 108

def add_capital_letter!(letters)
  sanitized = self
  sanitized[rand(sanitized.length)] = letters[rand(letters.length)].upcase
  self.replace(sanitized)
end

#add_number!(numbers) ⇒ String

Add a number to given string

Parameters:

  • numbers (Array)

    list of numbers, preferably single digits (0-9)

Returns:

  • (String)

    replaces self with a number in the place another random character



98
99
100
101
102
# File 'lib/passr/generator.rb', line 98

def add_number!(numbers)
  sanitized = self
  sanitized[rand(sanitized.length)] = numbers[rand(numbers.length)]
  self.replace(sanitized)
end

#add_special!(special_chars) ⇒ String

Add a special character to given string

Parameters:

  • special_chars (Array)

    list of special characters

Returns:

  • (String)

    replaces self with a special character in the place another random character



88
89
90
91
92
# File 'lib/passr/generator.rb', line 88

def add_special!(special_chars)
  sanitized = self
  sanitized[rand(sanitized.length)] = special_chars[rand(special_chars.length)]
  self.replace(sanitized)
end

#unique_chars!String

Make string entirely unique characters

Returns:

  • (String)

    replaces self with a string that is made up of all unique characters



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/passr/generator.rb', line 117

def unique_chars!
  sanitized = self
  sanitized.length.times do |i|
    sanitized.chars.each_with_index do |char, index|
      unless i == index
        sanitized[i] = Passr::Generator.new_character(sanitized) if sanitized[i] == char
      end
    end
  end
  self.replace(sanitized)
end