Class: EnigmaMachine::Plugboard

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

Direct Known Subclasses

Reflector

Instance Method Summary collapse

Constructor Details

#initialize(mapping_pairs, rotor) ⇒ Plugboard

Construct a new plugboard

Example:

Plugboard.new(%w(AB CD EF GH), @rotor)

Parameters:

  • mapping_pairs (Array<String>)

    A list of letter pairs to be connected

  • rotor (#translate)

    the rightmost rotor that will be called next in the processing chain

Raises:

  • (ConfigurationError)

    if an invalid mapping passed in (not pairs of leters, or a letter connected more than once)



12
13
14
15
# File 'lib/enigma_machine/plugboard.rb', line 12

def initialize(mapping_pairs, rotor)
  build_mapping(mapping_pairs)
  @decorated = rotor
end

Instance Method Details

#substitute(letter) ⇒ String

Substitutes a letter according the configured plug pairs

Parameters:

  • letter (String)

    the letter to be substituted

Returns:

  • (String)

    the substituted letter



33
34
35
# File 'lib/enigma_machine/plugboard.rb', line 33

def substitute(letter)
  @mapping[letter] || letter
end

#translate(letter) ⇒ String

Translate a letter

This performs a substitution, calls the rotor to do the rest of the translation, and then substitutes the result on the way back out.

Parameters:

  • letter (String)

    the letter to be translated

Returns:

  • (String)

    the translated letter



24
25
26
27
28
# File 'lib/enigma_machine/plugboard.rb', line 24

def translate(letter)
  step = substitute(letter)
  step = @decorated.translate(step)
  substitute(step)
end