Class: Tape

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

Constant Summary collapse

BLANK =
'#'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tape

Returns a new instance of Tape.



7
8
9
10
# File 'lib/ntm/tape.rb', line 7

def initialize(options = {} )
  @head_position = options[:head_position].to_i
  @content       = (options[:content].to_s.empty?) ? [BLANK]: options[:content].split("")
end

Instance Attribute Details

#head_positionObject (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

#dupObject



34
35
36
# File 'lib/ntm/tape.rb', line 34

def dup
  Tape.new({head_position: @head_position, content: @content.dup.join})
end

#move_leftObject

Raises:

  • (StandardError)


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_rightObject



25
26
27
# File 'lib/ntm/tape.rb', line 25

def move_right
  @head_position += 1
end

#readObject



16
17
18
# File 'lib/ntm/tape.rb', line 16

def read
  @content[@head_position] || BLANK
end

#resetObject



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