Class: Tape
- Inherits:
-
Object
- Object
- Tape
- Defined in:
- lib/ntm/tape.rb
Constant Summary collapse
- BLANK =
'#'
Instance Attribute Summary collapse
-
#head_position ⇒ Object
readonly
Returns the value of attribute head_position.
Instance Method Summary collapse
- #dup ⇒ Object
-
#initialize(options = {}) ⇒ Tape
constructor
A new instance of Tape.
- #move_left ⇒ Object
- #move_right ⇒ Object
- #read ⇒ Object
- #reset ⇒ Object
- #write(symbol) ⇒ Object
Constructor Details
Instance Attribute Details
#head_position ⇒ Object (readonly)
Returns the value of attribute head_position.
5 6 7 |
# File 'lib/ntm/tape.rb', line 5 def head_position @head_position end |
Instance Method Details
#dup ⇒ Object
34 35 36 |
# File 'lib/ntm/tape.rb', line 34 def dup Tape.new({head_position: @head_position, content: @content.dup.join}) end |
#move_left ⇒ Object
20 21 22 23 |
# File 'lib/ntm/tape.rb', line 20 def move_left raise StandardError, 'The machine moved off the left-hand of the tape!' if @head_position == 0 @head_position -= 1 end |
#move_right ⇒ Object
25 26 27 |
# File 'lib/ntm/tape.rb', line 25 def move_right @head_position += 1 end |
#read ⇒ Object
16 17 18 |
# File 'lib/ntm/tape.rb', line 16 def read @content[@head_position] || BLANK end |
#reset ⇒ Object
29 30 31 32 |
# File 'lib/ntm/tape.rb', line 29 def reset @content = [BLANK] @head_position = 0 end |
#write(symbol) ⇒ Object
12 13 14 |
# File 'lib/ntm/tape.rb', line 12 def write(symbol) @content[@head_position] = symbol end |