Class: BoardGameGrid::Direction
- Inherits:
-
Object
- Object
- BoardGameGrid::Direction
- Defined in:
- lib/board_game_grid/direction.rb
Overview
Direction
The Direction that something is moving on a 2d plane
Instance Attribute Summary collapse
-
#x ⇒ Fixnum
readonly
The x magnitude.
-
#y ⇒ Fixnum
readonly
The y magnitude.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if directions are equal by seeing if their magnitudes are equal.
-
#initialize(dx, dy) ⇒ Direction
constructor
New objects can be instantiated with.
Constructor Details
#initialize(dx, dy) ⇒ Direction
New objects can be instantiated with
Example:
# Instantiates a new Direction
BoardGameGrid::Direction.new(1, 1)
19 20 21 22 23 24 |
# File 'lib/board_game_grid/direction.rb', line 19 def initialize(dx, dy) x = dx == 0 ? dx : dx/dx.abs y = dy == 0 ? dy : dy/dy.abs @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Fixnum (readonly)
Returns the x magnitude.
27 28 29 |
# File 'lib/board_game_grid/direction.rb', line 27 def x @x end |
#y ⇒ Fixnum (readonly)
Returns the y magnitude.
30 31 32 |
# File 'lib/board_game_grid/direction.rb', line 30 def y @y end |
Instance Method Details
#==(other) ⇒ Boolean
Check if directions are equal by seeing if their magnitudes are equal.
38 39 40 |
# File 'lib/board_game_grid/direction.rb', line 38 def ==(other) self.x == other.x && self.y == other.y end |