Class: Conway::Point
- Inherits:
-
Object
- Object
- Conway::Point
- Defined in:
- lib/conway/point.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #adjacents ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(x = 0, y = 0) ⇒ Point
constructor
A new instance of Point.
- #update(x, y) ⇒ Object
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
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/conway/point.rb', line 3 def x @x end |
#y ⇒ Object
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 |
#adjacents ⇒ Object
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
9 10 11 |
# File 'lib/conway/point.rb', line 9 def eql?(other) self.x == other.x && self.y == other.y end |
#hash ⇒ Object
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 |