Class: Instrumentation::BoundedArray

Inherits:
Object
  • Object
show all
Defined in:
lib/instrumentation/bounded_array.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size, items = []) ⇒ BoundedArray

Returns a new instance of BoundedArray.



8
9
10
11
# File 'lib/instrumentation/bounded_array.rb', line 8

def initialize(max_size, items = [])
  @max_size = max_size
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



6
7
8
# File 'lib/instrumentation/bounded_array.rb', line 6

def items
  @items
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



5
6
7
# File 'lib/instrumentation/bounded_array.rb', line 5

def max_size
  @max_size
end

Instance Method Details

#<<(item) ⇒ Object



13
14
15
16
17
18
# 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