Class: Pry::Ring

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb

Overview

A ring is a thread-safe fixed-capacity array to which you can only add elements. Older entries are overwritten as you add new elements, so that the ring can never contain more than ‘max_size` elemens.

Examples:

ring = Pry::Ring.new(3)
ring << 1 << 2 << 3
ring.to_a #=> [1, 2, 3]
ring << 4
ring.to_a #=> [2, 3, 4]

ring[0] #=> 2
ring[-1] #=> 4
ring.clear
ring[0] #=> nil

Since:

  • v0.12.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Ring

Returns a new instance of Ring.

Parameters:

  • max_size (Integer)

    Maximum buffer size. The buffer will start overwriting elements once its reaches its maximum capacity

Since:

  • v0.12.0



33
34
35
36
37
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 33

def initialize(max_size)
  @max_size = max_size
  @mutex = Mutex.new
  clear
end

Instance Attribute Details

#countInteger (readonly) Also known as: size

Returns how many objects were added during the lifetime of the ring.

Returns:

  • (Integer)

    how many objects were added during the lifetime of the ring

Since:

  • v0.12.0



28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 28

def count
  @count
end

#max_sizeInteger (readonly)

Returns maximum buffer size.

Returns:

  • (Integer)

    maximum buffer size

Since:

  • v0.12.0



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 24

def max_size
  @max_size
end

Instance Method Details

#<<(value) ⇒ self

Push ‘value` to the current index.

Parameters:

Returns:

  • (self)

Since:

  • v0.12.0



43
44
45
46
47
48
49
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 43

def <<(value)
  @mutex.synchronize do
    @buffer[count % max_size] = value
    @count += 1
    self
  end
end

#[](index) ⇒ Object, ...

Read the value stored at ‘index`.

Parameters:

  • index (Integer, Range)

    The element (if Integer) or elements (if Range) associated with ‘index`

Returns:

Since:

  • v0.12.0



57
58
59
60
61
62
63
64
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 57

def [](index)
  @mutex.synchronize do
    return @buffer[index] if count <= max_size
    return @buffer[(count + index) % max_size] if index.is_a?(Integer)

    transpose_buffer_tail[index]
  end
end

#clearvoid

This method returns an undefined value.

Clear the buffer and reset count.

Since:

  • v0.12.0



75
76
77
78
79
80
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 75

def clear
  @mutex.synchronize do
    @buffer = []
    @count = 0
  end
end

#to_aArray<Object>

Returns the buffer as unwinded array.

Returns:

Since:

  • v0.12.0



67
68
69
70
71
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/ring.rb', line 67

def to_a
  return @buffer.dup if count <= max_size

  transpose_buffer_tail
end