class Textbringer::Ring
Public Class Methods
new(max = 30, on_delete: ->(x) {}
click to toggle source
# File lib/textbringer/ring.rb, line 5 def initialize(max = 30, on_delete: ->(x) {}) @max = max @ring = [] @current = -1 @on_delete = on_delete end
Public Instance Methods
[](n = 0)
click to toggle source
# File lib/textbringer/ring.rb, line 48 def [](n = 0) @ring[get_index(n)] end
clear()
click to toggle source
# File lib/textbringer/ring.rb, line 12 def clear @ring.clear @current = -1 end
current()
click to toggle source
# File lib/textbringer/ring.rb, line 36 def current if @ring.empty? raise EditorError, "Ring is empty" end @ring[@current] end
each(&block)
click to toggle source
# File lib/textbringer/ring.rb, line 60 def each(&block) @ring.each(&block) end
empty?()
click to toggle source
# File lib/textbringer/ring.rb, line 52 def empty? @ring.empty? end
pop()
click to toggle source
# File lib/textbringer/ring.rb, line 30 def pop x = @ring[@current] rotate(1) x end
push(obj)
click to toggle source
# File lib/textbringer/ring.rb, line 17 def push(obj) @current += 1 if @ring.size < @max @ring.insert(@current, obj) else if @current == @max @current = 0 end @on_delete.call(@ring[@current]) @ring[@current] = obj end end
rotate(n)
click to toggle source
# File lib/textbringer/ring.rb, line 43 def rotate(n) @current = get_index(n) @ring[@current] end
size()
click to toggle source
# File lib/textbringer/ring.rb, line 56 def size @ring.size end
to_a()
click to toggle source
# File lib/textbringer/ring.rb, line 64 def to_a @ring.to_a end
Private Instance Methods
get_index(n)
click to toggle source
# File lib/textbringer/ring.rb, line 70 def get_index(n) if @ring.empty? raise EditorError, "Ring is empty" end i = @current - n if 0 <= i && i < @ring.size i else i % @ring.size end end