Class: GameOfLife::Plane

Inherits:
Array
  • Object
show all
Defined in:
lib/game_of_life/plane.rb

Overview

Custom Plane class that represent a cyclic/circular plane/array This class would always return an element unless the array is empty

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object

Extending the default array behaviour by treating the array as cyclic/circular object

Examples:

working with normal array

a = [0, 1, 2, 3]
a[-6] == a[-2] == a[2]
a[-5] == a[-1] == a[3]
a[-4] == a[0]  == a[4]
a[-3] == a[1]  == a[5]
a[-2] == a[2]  == a[6]
a[-1] == a[3]  == a[7]
a[0]  == a[4]  == a[8]

working with empty array

b = []
b[0] == b[1] == b[-1] == nil


22
23
24
25
# File 'lib/game_of_life/plane.rb', line 22

def [](index)
  return nil if empty?
  super(index % size)
end