Class: Conway::Point

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Point

Returns a new instance of Point.



5
6
7
# File 'lib/conway/point.rb', line 5

def initialize(x=0,y=0)
  self.x, self.y = x,y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/conway/point.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/conway/point.rb', line 3

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
# File 'lib/conway/point.rb', line 13

def ==(other)
  eql? other
end

#adjacentsObject



29
30
31
32
33
34
35
36
# File 'lib/conway/point.rb', line 29

def adjacents
  @adjacents ||=
  (-1..1).map do |j|
    (-1..1).map do |i|
      Point.new(x+i, y+j) unless i == 0 && j == 0
    end
  end.flatten.compact
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/conway/point.rb', line 9

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

#hashObject



17
18
19
# File 'lib/conway/point.rb', line 17

def hash
  :"#{x}-#{y}".object_id
end

#update(x, y) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/conway/point.rb', line 21

def update(x, y)
  self.x     = x
  self.y     = y
  @adjacents = nil

  self
end