Class: Position

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(col, row) ⇒ Position

Returns a new instance of Position.



6
7
8
9
# File 'lib/position.rb', line 6

def initialize(col, row)
  @col = col
  @row = row
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



4
5
6
# File 'lib/position.rb', line 4

def col
  @col
end

#rowObject (readonly)

Returns the value of attribute row.



4
5
6
# File 'lib/position.rb', line 4

def row
  @row
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  [col, row] == [other.col, other.row]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/position.rb', line 31

def eql?(other)
  [col, row].eql?([other.col, other.row])
end

#hashObject



35
36
37
# File 'lib/position.rb', line 35

def hash
  [col, row].hash
end

#next(direction) ⇒ Object



23
24
25
# File 'lib/position.rb', line 23

def next(direction)
  shift(1, direction)
end

#previous(direction) ⇒ Object



19
20
21
# File 'lib/position.rb', line 19

def previous(direction)
  shift(-1, direction)
end

#shift(offset, direction) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/position.rb', line 11

def shift(offset, direction)
  if direction == Direction::HORIZONTAL
    Position.new(col + offset, row)
  else
    Position.new(col, row + offset)
  end
end