0.01
No commit activity in last 3 years
No release in over 3 years
Adds scopes to ActiveRecord to allow for biased and weighted random sampling from a query.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 4.2.0
 Project Readme

StreamSampler - a gem to add stream sampling to Ruby classes.

This gem adds stream sampling (aka reservoir sampling) to Ruby. To use, add the gem to your Gemfile (or require it explicitly), and call the StreamSampler.reservoir_sample method:

require 'stream_sampler`
# takes a 10 item sample from a stream of items:
items = (1..1000).to_a
StreamSampler.reservoir_sample(items, 10)

As a special case, if ActiveRecord and ActiveSupport are defined, the stream sampling methods will be added as class methods on ActiveRecord::Base, so you can do things like this:

require 'stream_sampler'
User.where(age: (18..65)).reservoir_sample(10)