0.0
No commit activity in last 3 years
No release in over 3 years
Sets with conditional membership defined by a given block.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.0
~> 10.0
 Project Readme

RestrictedSet Build Status

RestricedSet implements a set with restrictions defined by a given block. If the block's arity is 2, it is called with the RestrictedSet itself and an object to see if the object is allowed to be put in the set. Otherwise, the block is called with an object to see if the object is allowed to be put in the set.

Credit: lib/set.rb

This project is inspired by a data structure described in the Ruby codebase. The interface matches the described class and the implementation is complete, tested and avoids dynamically defining methods on each instance.

Compatible with Ruby 1.9+

The source is released under the same terms as Ruby.

Installation

Add this line to your application's Gemfile:

gem 'restricted_set'

And then execute:

$ bundle

Or install it yourself as:

$ gem install restricted_set

Usage

set = RestrictedSet.new(-2..2) { |o| o > 0 }
set.add(-1)
set.add(42)
set #=> #<RestrictedSet: {1, 2, 42}>
set = RestrictedSet.new { |o| Symbol === o }
set.add("Hello!")
set.add(:name)
set.add(99)
set.add(:location)
set #=> #<RestrictedSet: {:name, :location}>
set = RestrictedSet.new do |current_set, _|
  current_set.count + 1 <= 2 # Maximum of 2 objects
end
set << :a << :b << :c
set #=> #<RestrictedSet: {:a, :b}>
require 'prime'
class PrimeSet < RestrictedSet
  def initialize(enum = nil)
    super enum, &Prime.method(:prime?)
  end
end

(1..100).to_set(PrimeSet) #=> #<PrimeSet: {2, 3, 5, 7, 11, 13, 17, 19, ...}>
class NullSet < RestrictedSet
  def initialize(enum = nil)
    super(enum) { false }
  end
end

NullSet[1, 'a', :b] #=> #<NullSet: {}>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request