class TuringMachine::Tape

Public: The tape of a Turing machine, combined with the head.

Constants

BLANK_SYMBOL

Attributes

index[R]

Public Class Methods

new(data = BLANK_SYMBOL) click to toggle source
# File lib/turing_machine/tape.rb, line 8
def initialize(data = BLANK_SYMBOL)
  @symbols = data.scan(/./)
  @index = 0
end

Public Instance Methods

head() click to toggle source
# File lib/turing_machine/tape.rb, line 15
def head
  @symbols[@index]
end
head=(symbol) click to toggle source
# File lib/turing_machine/tape.rb, line 19
def head=(symbol)
  @symbols[@index] = symbol
end
shift_left() click to toggle source

Public: Move the head to the left.

# File lib/turing_machine/tape.rb, line 24
def shift_left
  if @index == 0
    @symbols.unshift(BLANK_SYMBOL)
  else
    @index -= 1
  end
end
shift_right() click to toggle source

Public: Move the head to the right.

# File lib/turing_machine/tape.rb, line 33
def shift_right
  @symbols.push(BLANK_SYMBOL) if @index == @symbols.size - 1
  @index += 1
end
to_s() click to toggle source
# File lib/turing_machine/tape.rb, line 38
def to_s
  @symbols.join
end