Class: History
- Inherits:
-
Object
- Object
- History
- Defined in:
- lib/tools/history.rb
Overview
A History object, which holds the last size objects in memory.
Implemented as a ring buffer which wraps over.
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #<<(element) ⇒ Object
-
#initialize(size) ⇒ History
constructor
A new instance of History.
-
#last_nth(idx) ⇒ Object
returns the nth-last entry.
Constructor Details
#initialize(size) ⇒ History
7 8 9 10 11 |
# File 'lib/tools/history.rb', line 7 def initialize(size) @buffer = Array.new(size) @size = size @writer = 0 end |
Instance Attribute Details
#size ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/tools/history.rb', line 5 def size @size end |
Instance Method Details
#<<(element) ⇒ Object
21 22 23 24 |
# File 'lib/tools/history.rb', line 21 def <<(element) @writer = (@writer + 1) % @size @buffer[@writer] = element end |
#last_nth(idx) ⇒ Object
returns the nth-last entry
14 15 16 17 18 19 |
# File 'lib/tools/history.rb', line 14 def last_nth(idx) raise ArgumentError, "RingBuffer size #{@size} is too small to hold #{idx} entries" if idx > @size pos = (@writer - idx) % @size @buffer[pos] end |