class Instrumentation::BoundedArray

Array with bounded size. If the max size is exceeded it pops the first element and adds the new element at the end of the array

Attributes

items[R]
max_size[R]

Public Class Methods

new(max_size, items = []) click to toggle source
# File lib/instrumentation/bounded_array.rb, line 8
def initialize(max_size, items = [])
  @max_size = max_size
  @items = items
end

Public Instance Methods

<<(item) click to toggle source
# File lib/instrumentation/bounded_array.rb, line 13
def <<(item)
  slice = @items
  slice = @items[1..-1] if max_reached?

  BoundedArray.new(max_size, slice + [item])
end

Private Instance Methods

max_reached?() click to toggle source
# File lib/instrumentation/bounded_array.rb, line 22
def max_reached?
  items.size >= @max_size
end