Class: PrintSquare::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/print_square/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, size, size_offset = 1) ⇒ Vector

Returns a new instance of Vector.



5
6
7
8
9
# File 'lib/print_square/vector.rb', line 5

def initialize(direction, size, size_offset=1)
  @direction, @size, @size_offset = direction, size, size_offset
  @position = size.even? ? 0 : size - 1
  @offset = 0
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



3
4
5
# File 'lib/print_square/vector.rb', line 3

def direction
  @direction
end

#offsetObject

Returns the value of attribute offset.



3
4
5
# File 'lib/print_square/vector.rb', line 3

def offset
  @offset
end

#positionObject

Returns the value of attribute position.



3
4
5
# File 'lib/print_square/vector.rb', line 3

def position
  @position
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/print_square/vector.rb', line 3

def size
  @size
end

#turnObject

Returns the value of attribute turn.



3
4
5
# File 'lib/print_square/vector.rb', line 3

def turn
  @turn
end

Instance Method Details

#bounds?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/print_square/vector.rb', line 16

def bounds?
  @position == (@direction == 1 ? (@size - @size_offset) : @offset) 
end

#nextObject



11
12
13
14
# File 'lib/print_square/vector.rb', line 11

def next
  @position += @direction
  @turn.call if bounds?
end