-
-
Save rlb3/5213989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CircularList < Array | |
| def index | |
| @index ||=0 | |
| @index.abs | |
| end | |
| def current | |
| @index ||= 0 | |
| get_at(@index) | |
| end | |
| def next(num=1) | |
| @index ||= 0 | |
| @index += num | |
| get_at(@index) | |
| end | |
| def previous(num=1) | |
| @index ||= 0 | |
| @index -= num | |
| get_at(@index) | |
| end | |
| private | |
| def get_at(index) | |
| if index >= 0 | |
| at(index % self.size) | |
| else | |
| index = self.size + index | |
| get_at(index) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment