Class: SizedList

Inherits:
Array
  • Object
show all
Defined in:
lib/production_log/analyzer.rb

Overview

A list that only stores limit items.

Direct Known Subclasses

SlowestTimes

Instance Method Summary collapse

Constructor Details

#initialize(limit, &delete_block) ⇒ SizedList

Creates a new SizedList that can hold up to limit items. Whenever adding a new item to the SizedList would make the list larger than limit, delete_block is called.

delete_block is passed the list and the item being added. delete_block must take action to remove an item and return true or return false if the item should not be added to the list.



59
60
61
62
# File 'lib/production_log/analyzer.rb', line 59

def initialize(limit, &delete_block)
  @limit = limit
  @delete_block = delete_block
end

Instance Method Details

#<<(obj) ⇒ Object

Attempts to add obj to the list.



67
68
69
70
# File 'lib/production_log/analyzer.rb', line 67

def <<(obj)
  return super if self.length < @limit
  return super if @delete_block.call self, obj
end