Class: TuringMachine::Tape

Inherits:
Object
  • Object
show all
Defined in:
lib/turing_machine/tape.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTape

Returns a new instance of Tape.



6
7
8
9
10
11
# File 'lib/turing_machine/tape.rb', line 6

def initialize
  # @symbols = [ '0' ]
  # @index = 0
  @symbols = Array.new(40) { '0' }
  @index = 19
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



13
14
15
# File 'lib/turing_machine/tape.rb', line 13

def index
  @index
end

Instance Method Details

#headObject

def size

@symbols.size

end



19
20
21
# File 'lib/turing_machine/tape.rb', line 19

def head
  @symbols[@index]
end

#head=(symbol) ⇒ Object



23
24
25
# File 'lib/turing_machine/tape.rb', line 23

def head=(symbol)
  @symbols[@index] = symbol
end

#shift_leftObject



27
28
29
# File 'lib/turing_machine/tape.rb', line 27

def shift_left
  @index -= 1
end

#shift_rightObject



31
32
33
# File 'lib/turing_machine/tape.rb', line 31

def shift_right
  @index += 1
end

#to_sObject



35
36
37
# File 'lib/turing_machine/tape.rb', line 35

def to_s
  @symbols.join
end