Class: RingBuffer

Inherits:
Array show all
Defined in:
lib/scout_realtime/lib/ring_buffer.rb

Instance Method Summary collapse

Methods inherited from Array

#bsearch, #combination, #cycle, #extract_options!, #flatten_with_optional_argument, #flatten_with_optional_argument!, #index_with_block, #keep_if, #permutation, #pop_with_optional_argument, #product, #product_with_block, #repeated_combination, #repeated_permutation, #rindex_with_block, #rotate, #rotate!, #sample, #select!, #shift_with_optional_argument, #shuffle, #shuffle!, #sort_by!, try_convert, #uniq_with_block, #uniq_with_block!

Constructor Details

#initialize(size) ⇒ RingBuffer

Returns a new instance of RingBuffer.



6
7
8
9
# File 'lib/scout_realtime/lib/ring_buffer.rb', line 6

def initialize( size )
  @ring_size = size
  super( size )
end

Instance Method Details

#[](offset = 0) ⇒ Object

Access elements in the RingBuffer

offset will be typically negative!



22
23
24
# File 'lib/scout_realtime/lib/ring_buffer.rb', line 22

def []( offset = 0 )
  return self.array_element( - 1 + offset )
end

#array_elementObject



4
# File 'lib/scout_realtime/lib/ring_buffer.rb', line 4

alias_method :array_element, :[]

#array_pushObject



3
# File 'lib/scout_realtime/lib/ring_buffer.rb', line 3

alias_method :array_push, :push

#push(element) ⇒ Object



11
12
13
14
15
16
# File 'lib/scout_realtime/lib/ring_buffer.rb', line 11

def push( element )
  if length == @ring_size
    shift # loose element
  end
  array_push element
end