Class: PasswordAndOrdinals

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

Instance Method Summary collapse

Constructor Details

#initialize(password, ordinals) ⇒ PasswordAndOrdinals

Returns a new instance of PasswordAndOrdinals.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/password_and_ordinals.rb', line 7

def initialize(password, ordinals)
  raise(ArgumentError, "#{ordinals} does not ask for any characters") if ordinals.empty?
  raise(ArgumentError, "#{ordinals} only asks for one character") if ordinals.one?

  raise(ArgumentError, "#{ordinals} is not in ascending order") unless sorted?(ordinals)

  raise(ArgumentError, "#{ordinals.uniq.to_sentence} duplicated in #{ordinals}") unless uniq?(ordinals)
  raise(ArgumentError, "#{ordinals} would reveal entire password") unless ordinals.size < password.size # needs to come after checking duplicates

  raise(ArgumentError, "#{ordinals_out_of_bounds(password, ordinals).to_sentence} out of bounds") unless ordinals.all? { |i| i.in? ordinal_range(password) }

  @password_array = password.chars
  @indices = indices ordinals
end

Instance Method Details

#charactersObject



22
23
24
# File 'lib/password_and_ordinals.rb', line 22

def characters
  @indices.map { |i| @password_array.at i }
end