diff options
| author | a14m <[email protected]> | 2020-11-30 20:02:18 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-11-30 20:02:18 +0100 |
| commit | d8aff6caa4c9872de1d437faad00191c9d099a7a (patch) | |
| tree | 8b062c01b32bb68b810762ea62ec71b6186a9ded /CLI/ruby/lib | |
| parent | 553cde4d33c860a5490691117e414e586a685ae9 (diff) | |
Add the cyclic plane (circular array) implementation w/testing
Diffstat (limited to 'CLI/ruby/lib')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/plane.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/CLI/ruby/lib/game_of_life/plane.rb b/CLI/ruby/lib/game_of_life/plane.rb new file mode 100644 index 0000000..f866754 --- /dev/null +++ b/CLI/ruby/lib/game_of_life/plane.rb @@ -0,0 +1,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 |
