Class: EnigmaMachine::Reflector

Inherits:
Plugboard show all
Defined in:
lib/enigma_machine/reflector.rb

Constant Summary collapse

STANDARD_MAPPINGS =
{
  :A => %w(AE BJ CM DZ FL GY HX IV KW NR OQ PU ST),
  :B => %w(AY BR CU DH EQ FS GL IP JX KN MO TZ VW),
  :C => %w(AF BV CP DJ EI GO HY KR LZ MX NW TQ SU),
  :Bthin => %w(AE BN CK DQ FU GY HW IJ LO MP RX SZ TV),
  :Cthin => %w(AR BD CO EJ FN GT HK IV LM PW QZ SX UY),
}

Instance Method Summary collapse

Methods inherited from Plugboard

#substitute

Constructor Details

#initialize(mapping) ⇒ Reflector

Construct a new reflector

Examples:

Reflector.new(:B)
Reflector.new(%w(AY BR CU DH EQ FS GL IP JX KN MO TZ VW))

Parameters:

  • mapping (Symbol, Array<String>)

    A symbol representing one of the standard reflectors or an array of 13 letter pairs to be swapped by the reflector

Raises:

  • (ConfigurationError)

    if an invalid mapping is passed in (unrecognised standard reflector, not 13 pairs of letters, or a letter appearing more than once)



22
23
24
25
26
27
28
29
# File 'lib/enigma_machine/reflector.rb', line 22

def initialize(mapping)
  if mapping.is_a?(Symbol)
    raise ConfigurationError unless STANDARD_MAPPINGS.has_key?(mapping)
    mapping = STANDARD_MAPPINGS[mapping]
  end
  raise ConfigurationError unless mapping.length == 13
  build_mapping(mapping)
end