# frozen_string_literal: true module GameOfLife # Custom Plane class that represent a cyclic/circular plane/array # This class would always return an element unless the array is empty class Plane < Array # Extending the default array behaviour by treating the array as cyclic/circular object # # @example 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] # # @example working with empty array # b = [] # b[0] == b[1] == b[-1] == nil def [](index) return nil if empty? super(index % size) end end end