Class: Enigma::Reflector

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

Overview

The engima reflector is used to scramble the input. It takes input, performs a substituion, then returns output. The scramling functions such that if 'A' maps to 'B' then 'B' maps to 'A' (thus allowing for both encrypting and decrypting).

Usage:

reflector = Enigma::Reflector.new
machine.reflect('A') # 'B'
machine.reflect('B') # 'A'
...

Defined Under Namespace

Modules: Mappings, Position

Instance Method Summary collapse

Constructor Details

#initialize(mappings: Mappings::DEFAULT, position: Position::DEFAULT) ⇒ Reflector

Returns a new instance of Reflector.


25
26
27
28
# File 'lib/enigma/reflector.rb', line 25

def initialize(mappings: Mappings::DEFAULT, position: Position::DEFAULT)
  @mappings = mappings
  @position = position
end

Instance Method Details

#reflect(letter) ⇒ Object


30
31
32
33
34
# File 'lib/enigma/reflector.rb', line 30

def reflect(letter)
  return letter unless ALPHABET.include?(letter)

  ALPHABET[(@mappings.index(letter) + @position) % ALPHABET.length]
end