class Rbtm::Head

The read/write head of the Turing machine.

Attributes

index[R]
state[R]
tape[R]

Public Class Methods

new(str, state) click to toggle source
# File lib/rbtm/head.rb, line 8
def initialize(str, state)
  @tape = (str.empty? ? '_' : str.dup)
  @state = state
  @index = 0
end

Public Instance Methods

operate(rule) click to toggle source

Operates on the tape using the given rule.

# File lib/rbtm/head.rb, line 16
def operate(rule)
  @state = rule[0]
  write(rule[1])

  case rule[2]
  when 'L'
    left
  when 'R'
    right
  end
end
signature() click to toggle source
# File lib/rbtm/head.rb, line 28
def signature
  [state, read]
end
to_s() click to toggle source
# File lib/rbtm/head.rb, line 32
def to_s
  tape.sub(/^_+/, '').sub(/_+$/, '')
end

Private Instance Methods

left() click to toggle source
# File lib/rbtm/head.rb, line 38
def left
  if index.zero?
    pad
  else
    @index -= 1
  end
end
pad() click to toggle source
# File lib/rbtm/head.rb, line 46
def pad
  tape.insert(index, '_')
end
read() click to toggle source
# File lib/rbtm/head.rb, line 50
def read
  tape[index]
end
right() click to toggle source
# File lib/rbtm/head.rb, line 54
def right
  @index += 1
  pad if index == tape.size
end
write(char) click to toggle source
# File lib/rbtm/head.rb, line 59
def write(char)
  tape[index] = char
end