Class: Enigma::Machine

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

Overview

The machine is composed of a number of rotors, a reflector, a plugboard and can encrypt a regular string or decrypt a converted string. Conversion is run through the same method for enciphering or deciphering as long as the machine is configured identically.

Usage:

machine = Enigma::Machine.new
machine.convert('TURING') # '.....'
machine.reset!
machine.convert('.....') # 'TURING'

Constant Summary collapse

SEPARATOR =
''.freeze

Instance Method Summary collapse

Constructor Details

#initialize(rotors: Rotor.rotors, reflector: Reflector.new, plugboard: Plugboard.new) ⇒ Machine

Returns a new instance of Machine.


17
18
19
20
21
# File 'lib/enigma/machine.rb', line 17

def initialize(rotors: Rotor.rotors, reflector: Reflector.new, plugboard: Plugboard.new)
  @rotors = rotors
  @reflector = reflector
  @plugboard = plugboard
end

Instance Method Details

#convert(string) ⇒ Object


23
24
25
# File 'lib/enigma/machine.rb', line 23

def convert(string)
  format(string).split(SEPARATOR).map { |character| process(character) }.join(SEPARATOR)
end

#reset!Object


27
28
29
# File 'lib/enigma/machine.rb', line 27

def reset!
  @rotors.each(&:reset!)
end