Class: Automata::Tape
- Inherits:
-
Object
- Object
- Automata::Tape
- Defined in:
- lib/automata/turing.rb
Overview
Turing Machine tape
Instance Attribute Summary collapse
-
#memory ⇒ Object
Returns the value of attribute memory.
Instance Method Summary collapse
-
#initialize(string = nil) ⇒ Tape
constructor
Creates a new tape.
-
#output ⇒ String
Trims the memory empty cells and outputs the tape string.
-
#transition(read, write, move) ⇒ Boolean
Transitions the tape.
Constructor Details
#initialize(string = nil) ⇒ Tape
Creates a new tape.
113 114 115 116 117 118 119 120 121 |
# File 'lib/automata/turing.rb', line 113 def initialize(string=nil) if string @memory = [] @memory << '@' unless string[0] == '@' @memory += string.split('') @memory << '@' unless string[-1] == '@' @head = 1 end end |
Instance Attribute Details
#memory ⇒ Object
Returns the value of attribute memory.
108 109 110 |
# File 'lib/automata/turing.rb', line 108 def memory @memory end |
Instance Method Details
#output ⇒ String
Trims the memory empty cells and outputs the tape string.
152 153 154 |
# File 'lib/automata/turing.rb', line 152 def output @memory.join.sub(/^@*/, '').sub(/@*$/, '') end |
#transition(read, write, move) ⇒ Boolean
Transitions the tape.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/automata/turing.rb', line 130 def transition(read, write, move) if read == @memory[@head] @memory[@head] = write case move when 'R' # Move right @memory << '@' if @memory[@head + 1] @head += 1 when 'L' # Move left @memory.unshift('@') if @head == 0 @head -= 1 end return true else return false end end |