Class: Pry::Ring
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` elements.
Instance Attribute Summary collapse
-
#count ⇒ Integer
(also: #size)
readonly
How many objects were added during the lifetime of the ring.
-
#max_size ⇒ Integer
readonly
Maximum buffer size.
Instance Method Summary collapse
-
#<<(value) ⇒ self
Push ‘value` to the current index.
-
#[](index) ⇒ Object, ...
Read the value stored at ‘index`.
-
#clear ⇒ void
Clear the buffer and reset count.
-
#initialize(max_size) ⇒ Ring
constructor
A new instance of Ring.
-
#to_a ⇒ Array<Object>
The buffer as unwinded array.
Constructor Details
#initialize(max_size) ⇒ Ring
Returns a new instance of Ring.
33 34 35 36 37 |
# File 'lib/pry/ring.rb', line 33 def initialize(max_size) @max_size = max_size @mutex = Mutex.new clear end |
Instance Attribute Details
#count ⇒ Integer (readonly) Also known as: size
Returns how many objects were added during the lifetime of the ring.
28 29 30 |
# File 'lib/pry/ring.rb', line 28 def count @count end |
#max_size ⇒ Integer (readonly)
Returns maximum buffer size.
24 25 26 |
# File 'lib/pry/ring.rb', line 24 def max_size @max_size end |
Instance Method Details
#<<(value) ⇒ self
Push ‘value` to the current index.
43 44 45 46 47 48 49 |
# File '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`.
57 58 59 60 61 62 63 64 |
# File '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 |
#clear ⇒ void
This method returns an undefined value.
Clear the buffer and reset count.
75 76 77 78 79 80 |
# File 'lib/pry/ring.rb', line 75 def clear @mutex.synchronize do @buffer = [] @count = 0 end end |
#to_a ⇒ Array<Object>
Returns the buffer as unwinded array.
67 68 69 70 71 |
# File 'lib/pry/ring.rb', line 67 def to_a return @buffer.dup if count <= max_size transpose_buffer_tail end |