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

new(opts={}, &block) click to toggle source

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

cipher(the_string="")

{cipher} is a convenience alias for {encipher}

Alias for: encipher
clear_plugboard() click to toggle source

Remove all connections from the plugboard.

# File lib/rotor_machine/session.rb, line 106
def clear_plugboard
  @machine.plugboard = RotorMachine::Plugboard.new
end
clear_rotors() click to toggle source

Remove all rotors from the machine.

# File lib/rotor_machine/session.rb, line 100
def clear_rotors
  @machine.rotors = []
end
connect(from, to) click to toggle source

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
Also aliased as: plug
default_machine() click to toggle source

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(from) click to toggle source

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
Also aliased as: unplug
empty_machine() click to toggle source

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(the_string="") click to toggle source

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
Also aliased as: encode, cipher
encode(the_string="")

{encode} is a convenience alias for {encipher}

Alias for: encipher
last_result() click to toggle source

Return the results of the last {encipher} operation, or nil.

# File lib/rotor_machine/session.rb, line 132
def last_result
  @last_result
end
machine() click to toggle source

Return the inner {RotorMachine::Machine} object.

# File lib/rotor_machine/session.rb, line 126
def machine
  @machine
end
Also aliased as: the_machine
plug(from, to)

{plug} is a convenience alias for {connect}

Alias for: connect
reflector(kind, position="A") click to toggle source

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
rotor(kind, position=0, step_size=1) click to toggle source

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_positions(pos_string) click to toggle source

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
Also aliased as: set_rotors
set_rotors(pos_string)

{set_rotors} is a convenience alias for {set_positions}

Alias for: set_positions
the_machine()

{the_machine} is a convenience alias for {machine}

Alias for: machine
unplug(from)

{unplug} is a convenience alias for {disconnect}

Alias for: disconnect