Class: Expeditor::RingBuffer
- Inherits:
-
Object
- Object
- Expeditor::RingBuffer
- Defined in:
- lib/expeditor/ring_buffer.rb
Overview
Circular buffer with user-defined initialization and optimized ‘move` implementation. The next element will be always initialized with user-defined initialization proc.
Thread unsafe.
Instance Method Summary collapse
-
#all ⇒ Array<Object>
Array of elements.
-
#current ⇒ Object
User created object with given initialization proc.
-
#initialize(size, &initialize_proc) ⇒ RingBuffer
constructor
A new instance of RingBuffer.
-
#move(times) ⇒ Object
Current element after moving.
Constructor Details
#initialize(size, &initialize_proc) ⇒ RingBuffer
Returns a new instance of RingBuffer.
9 10 11 12 13 14 15 |
# File 'lib/expeditor/ring_buffer.rb', line 9 def initialize(size, &initialize_proc) raise ArgumentError.new('initialize_proc is not given') unless initialize_proc @size = size @initialize_proc = initialize_proc @elements = Array.new(@size, &initialize_proc) @current_index = 0 end |
Instance Method Details
#all ⇒ Array<Object>
Returns Array of elements.
31 32 33 |
# File 'lib/expeditor/ring_buffer.rb', line 31 def all @elements end |
#current ⇒ Object
Returns user created object with given initialization proc.
18 19 20 |
# File 'lib/expeditor/ring_buffer.rb', line 18 def current @elements[@current_index] end |
#move(times) ⇒ Object
Returns current element after moving.
24 25 26 27 28 |
# File 'lib/expeditor/ring_buffer.rb', line 24 def move(times) cut_moving_time_if_possible(times).times do next_element end end |