Class: Mapleseed::Ring

Inherits:
Object
  • Object
show all
Defined in:
lib/mapleseed/ring.rb

Overview

base class for whirl command rings

Direct Known Subclasses

MathRing, OpRing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interpreter) ⇒ Ring

initialize the ring



8
9
10
11
12
13
14
# File 'lib/mapleseed/ring.rb', line 8

def initialize(interpreter)
	@position = 0
	@value = 0
	@direction = 1
	@commands = []
	@interpreter = interpreter
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/mapleseed/ring.rb', line 5

def commands
  @commands
end

#directionObject (readonly)

Returns the value of attribute direction.



5
6
7
# File 'lib/mapleseed/ring.rb', line 5

def direction
  @direction
end

#positionObject (readonly)

Returns the value of attribute position.



5
6
7
# File 'lib/mapleseed/ring.rb', line 5

def position
  @position
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/mapleseed/ring.rb', line 5

def value
  @value
end

Instance Method Details

#executeObject



31
32
33
34
35
# File 'lib/mapleseed/ring.rb', line 31

def execute
	if @position < @commands.length
		send @commands[@position]
	end
end

#rotateObject

rotate the ring in the given direction



22
23
24
25
26
27
28
29
# File 'lib/mapleseed/ring.rb', line 22

def rotate
	@position += @direction
	if @position == @commands.length
		@position = 0
	elsif @position == -1
		@position = @commands.length - 1
	end
end

#switch_directionObject

change the direction of the ring



17
18
19
# File 'lib/mapleseed/ring.rb', line 17

def switch_direction
	@direction *= -1
end