Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created April 29, 2012 05:09
Show Gist options
  • Save sausheong/2533435 to your computer and use it in GitHub Desktop.
Save sausheong/2533435 to your computer and use it in GitHub Desktop.
Circular doubly-linked list
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