Class: Reline::KillRing::RingBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/reline/kill_ring.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max = 1024) ⇒ RingBuffer

Returns a new instance of RingBuffer.



25
26
27
28
29
# File 'lib/reline/kill_ring.rb', line 25

def initialize(max = 1024)
  @max = max
  @size = 0
  @head = nil # reading head of ring-shaped tape
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



23
24
25
# File 'lib/reline/kill_ring.rb', line 23

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



22
23
24
# File 'lib/reline/kill_ring.rb', line 22

def size
  @size
end

Instance Method Details

#<<(point) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reline/kill_ring.rb', line 31

def <<(point)
  if @size.zero?
    @head = point
    @head.backward = @head
    @head.forward = @head
    @size = 1
  elsif @size >= @max
    tail = @head.forward
    new_tail = tail.forward
    @head.forward = point
    point.backward = @head
    new_tail.backward = point
    point.forward = new_tail
    @head = point
  else
    tail = @head.forward
    @head.forward = point
    point.backward = @head
    tail.backward = point
    point.forward = tail
    @head = point
    @size += 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/reline/kill_ring.rb', line 56

def empty?
  @size.zero?
end