Skip to content

Instantly share code, notes, and snippets.

@rlb3
Forked from sausheong/circularlist.rb
Created March 21, 2013 15:36
Show Gist options
  • Select an option

  • Save rlb3/5213989 to your computer and use it in GitHub Desktop.

Select an option

Save rlb3/5213989 to your computer and use it in GitHub Desktop.
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