Sha256: 5c08af91a1ae25ac7cb9eada823de25e9e95ce94c6dcb306b75615d790cf51a7

Contents?: true

Size: 551 Bytes

Versions: 3

Compression:

Stored size: 551 Bytes

Contents

module Instrumentation
  # 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
  class BoundedArray
    attr_reader :max_size
    attr_reader :items

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

    def <<(item)
      slice = @items
      slice = @items[1..-1] if max_reached?

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

    private

    def max_reached?
      items.size >= @max_size
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
process-instrumentation-0.1.3 lib/instrumentation/bounded_array.rb
process-instrumentation-0.1.2 lib/instrumentation/bounded_array.rb
process-instrumentation-0.1.1 lib/instrumentation/bounded_array.rb