Class: EnigmaRuby::Rotor
- Inherits:
-
Object
- Object
- EnigmaRuby::Rotor
- Defined in:
- lib/enigma_ruby/rotor.rb
Instance Method Summary collapse
- #advance ⇒ Object
- #at_notch? ⇒ Boolean
- #encode_backward(char) ⇒ Object
- #encode_forward(char) ⇒ Object
-
#initialize(wiring, position = 0) ⇒ Rotor
constructor
A new instance of Rotor.
Constructor Details
#initialize(wiring, position = 0) ⇒ Rotor
Returns a new instance of Rotor.
3 4 5 6 |
# File 'lib/enigma_ruby/rotor.rb', line 3 def initialize(wiring, position = 0) @wiring = wiring.chars @position = @notch = position end |
Instance Method Details
#advance ⇒ Object
18 19 20 |
# File 'lib/enigma_ruby/rotor.rb', line 18 def advance @position = (@position + 1) % wiring_size end |
#at_notch? ⇒ Boolean
22 23 24 |
# File 'lib/enigma_ruby/rotor.rb', line 22 def at_notch? @position == @notch end |
#encode_backward(char) ⇒ Object
13 14 15 16 |
# File 'lib/enigma_ruby/rotor.rb', line 13 def encode_backward(char) index = @wiring.index(char) ((index - @position) % wiring_size + 'A'.ord).chr end |
#encode_forward(char) ⇒ Object
8 9 10 11 |
# File 'lib/enigma_ruby/rotor.rb', line 8 def encode_forward(char) index = (char.ord - 'A'.ord + @position) % wiring_size @wiring[index] end |