class RotorMachine::Session
The {Session} object provides a very simple DSL for “conversational” interactions with the {RotorMachine::Machine} and its subordinate classes. This is useful for interactive and experimental applications, for testing, and so forth.
Eventually, a {Pry}-based REPL loop might be added to the project, but this functionality does not exist yet.
Instance methods are a loose wrapper around the rest of the library and are only loosely documented here. Argument validation is handled by simply bubbling up exceptions raised by the implementatation method.
Example Usage¶ ↑
RotorMachine.Session do default_machine set_rotors "AAA" connect "A", "G" encipher "THIS IS A SUPER SECRET MESSAGE" ct = last_result set_rotors "AAA" encipher ct puts last_result # THISI SASUP ERSEC RETME SSAGE end
Public Class Methods
Initialize the {RotorMachine::Session} instance. The methods of this object, except for {#machine} and {#last_result}, are primarily intended to be called within the {Session} block (via {instance_eval}).
@param opts [Hash] The setup options hash. Currently unused, but any options
provided are stored.
@param block [Block] The operations block. If provided, it is executed via
{instance_eval}. The instance is returned following execution of the block.
# File lib/rotor_machine/session.rb, line 39 def initialize(opts={}, &block) @opts = opts @machine = RotorMachine::Factory.empty_machine() @last_result = nil instance_eval(&block) if block_given? return self end
Public Instance Methods
Remove all connections from the plugboard.
# File lib/rotor_machine/session.rb, line 106 def clear_plugboard @machine.plugboard = RotorMachine::Plugboard.new end
Remove all rotors from the machine.
# File lib/rotor_machine/session.rb, line 100 def clear_rotors @machine.rotors = [] end
Connect a pair of letters on the machine's plugboard.
# File lib/rotor_machine/session.rb, line 66 def connect(from, to) @machine.plugboard.connect(from, to) end
Configure the machine to its default state (as in the {RotorMachine::Factory} object's {default_machine} method.)
# File lib/rotor_machine/session.rb, line 113 def default_machine @machine = RotorMachine::Factory.default_machine end
Disconnect a letter (and its inverse) from the machine's plugboard.
# File lib/rotor_machine/session.rb, line 72 def disconnect(from) @machine.plugboard.disconnect(from) end
Configure the machine to its empty state (as in the {RotorMachine::Factory} object's {empty_machine} method.)
# File lib/rotor_machine/session.rb, line 120 def empty_machine @machine = RotorMachine::Factory.empty_machine end
Encipher a string.
# File lib/rotor_machine/session.rb, line 78 def encipher(the_string="") if the_string.nil? res = "" elsif the_string.is_a?(Array) res = @machine.encipher(the_string.join(" ")) elsif the_string.is_a?(String) res = @machine.encipher(the_string) else res = @machine.encipher(the_string.to_s) end @last_result = res res end
Return the results of the last {encipher} operation, or nil.
# File lib/rotor_machine/session.rb, line 132 def last_result @last_result end
Return the inner {RotorMachine::Machine} object.
# File lib/rotor_machine/session.rb, line 126 def machine @machine end
Set the machine's reflector.
# File lib/rotor_machine/session.rb, line 58 def reflector(kind, position="A") r = RotorMachine::Factory.build_reflector reflector_kind: kind, initial_position: position @machine.reflector = r if r.is_a?(RotorMachine::Reflector) end
Create a rotor and add it to the machine.
# File lib/rotor_machine/session.rb, line 49 def rotor(kind, position=0, step_size=1) r = RotorMachine::Factory.build_rotor rotor_kind: kind, initial_position: position, step_size: step_size @machine.rotors << r if r.is_a?(RotorMachine::Rotor) end
Set the positions of the rotors.
# File lib/rotor_machine/session.rb, line 94 def set_positions(pos_string) @machine.set_rotors(pos_string) end
{set_rotors} is a convenience alias for {set_positions}