Class: BoardGameGrid::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/board_game_grid/point.rb

Overview

Point

A point with an x and y co-ordinates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Point

New objects can be instantiated with

Example:

# Instantiates a new Point
BoardGameGrid::Point.new({
  x: 1,
  y: 1
})

Parameters:

  • x (Fixnum)

    the x co-ordinate.

  • y (Fixnum)

    the y co-ordinate.



22
23
24
# File 'lib/board_game_grid/point.rb', line 22

def initialize(x, y)
  @x, @y = x, y
end

Instance Attribute Details

#xFixnum (readonly)

Returns the x co-ordinate.

Returns:

  • (Fixnum)

    the x co-ordinate.



27
28
29
# File 'lib/board_game_grid/point.rb', line 27

def x
  @x
end

#yFixnum (readonly)

Returns the y co-ordinate.

Returns:

  • (Fixnum)

    the y co-ordinate.



30
31
32
# File 'lib/board_game_grid/point.rb', line 30

def y
  @y
end

Instance Method Details

#+(other) ⇒ Point

Add a point to another point by adding their co-ordinates and returning a new point.

Parameters:

  • other (Point)

    the other point to add.

Returns:



38
39
40
# File 'lib/board_game_grid/point.rb', line 38

def +(other)
  self.class.new(self.x + other.x, self.y + other.y)
end

#==(other) ⇒ TrueClass, FalseClass

Check if popints are equal by seeing if their co-ordinates are equal.

Parameters:

  • other (Point)

    the other point to compare to.

Returns:

  • (TrueClass, FalseClass)


48
49
50
# File 'lib/board_game_grid/point.rb', line 48

def ==(other)
  self.x == other.x && self.y == other.y
end