top_n

Build Status Dependency Status

This gem allows tracking of the top N (or bottom N) keys added to a list. Each key can have an optional value, which is then added to a list of values for that key. It acts a lot like a norman Ruby Hash type, but with a limited number of keys based on sorting order.

Key types must respond to the usual comparison operators: <, <=, >, >=, and <=>. Values may be any object type, and need not be unique. Values are not examined in any way, simply added to an internal list.

If a <key, value> pair is added twice, it will store the value twice for that key, and return it twice in the list for that key, when that key is retrieved.

Once the top N keys are added, adding smaller keys will be ignored. Adding a larger key will drop the then-smallest value. Adding another value at an existing key will simply result in

For example, track the top 1,000 EverQuest 2 characters with the highest strenth, the most quests completed, or items crafted, out of the roughly 2.4 million characters which currently exist.

Contributing to this project

Copyright © 2013 Michael Graff. See LICENSE.txt for further details.