summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/lib/game_of_life/plane.rb
blob: f866754a66acfeaf39d2ff524bf657072e856fa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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