Class: EnigmaRuby::Enigma
- Inherits:
-
Object
- Object
- EnigmaRuby::Enigma
- Defined in:
- lib/enigma_ruby/enigma.rb
Instance Method Summary collapse
- #encrypt(cleartext) ⇒ Object
-
#initialize(rotor_settings = []) ⇒ Enigma
constructor
A new instance of Enigma.
Constructor Details
#initialize(rotor_settings = []) ⇒ Enigma
Returns a new instance of Enigma.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/enigma_ruby/enigma.rb', line 6 def initialize(rotor_settings = []) initialize_rotors(rotor_settings) @commands = [ Commands::SwapPlugboard.new, Commands::EncodeRotorForward.new(@rotors), Commands::Reflect.new, Commands::EncodeRotorBackward.new(@rotors), Commands::SwapPlugboard.new, Commands::AdvanceRotors.new(@rotors) ] end |
Instance Method Details
#encrypt(cleartext) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/enigma_ruby/enigma.rb', line 18 def encrypt(cleartext) encoded_text = '' cleartext.each_char do |char| next if SKIP_CHARACTERS_REGEX .match?(char) next encoded_text << char if NON_ENCRYPTED_CHARACTERS_REGEX .match?(char) context = { encoded_char: char } @commands.each { |command| command.execute(context) } encoded_text << context[:encoded_char] end encoded_text end |