Fork me on GitHub

go-ruby-set

Ruby's Set โ€” the unordered, unique collection with full set algebra โ€” in pure Go, no cgo.

Ruby Setset algebra | & - ^subset ยท superset ยท disjointclassify ยท divide ยท group_byinsertion-orderedHasher seamMRI byte-exact100% coverage6 arches
Documentation (MkDocs Material + mike) Go 1.26.4+ Coverage 100%
zsh
$ go get github.com/go-ruby-set/set@latest

go-ruby-set is a pure-Go (no cgo) reimplementation of Ruby's Set โ€” the unordered, unique collection with full set algebra from MRI's set standard library (autoloaded core in Ruby 4.0). It mirrors Set's observable behaviour: insertion-ordered iteration, add? / delete?, the | & - ^ operators, subset / superset / disjoint predicates, and classify / divide / group_by / flatten โ€” without any Ruby runtime. Element identity is supplied by the host through a Hasher seam, exactly as MRI keys members by hash / eql?. It is a standalone, reusable module โ€” the Set backend bound into go-embedded-ruby by rbgo, the same pattern as go-ruby-yaml โ€” differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Core & membership ready

Insertion-ordered storage with add / add?, delete / delete?, include?, size, empty?, clear, each, to_a, dup, merge and subtract โ€” keyed by member identity or through the host Hasher.

Set algebra ready

Union (|/+), Intersection (&), Difference (-) and XorSym (^) โ€” the four operators producing fresh sets that preserve first-insertion order.

Predicates ready

subset? / <=, proper_subset? / <, superset? / >=, proper_superset? / >, disjoint?, intersect? and ==, matching MRI’s comparison semantics exactly.

Enumeration & higher-order ready

map / collect, select / reject, in-place collect!, plus classify, group_by, both divide forms, recursive flatten, and sort via SortedSlice.

Element identity (Hasher) ready

A pluggable Hasher maps a member to the comparable Go key under which two members coincide โ€” rbgo plugs Ruby’s hash / eql?; plain New keys members by themselves.

Differential oracle & coverage ready

Set algebra, predicates, classify, both divide forms and the Set[โ€ฆ] inspect computed here and checked byte-for-byte against the system ruby (gated on Ruby โ‰ฅ 4.0); 100% coverage, gofmt + go vet clean, green across 6 arches and 3 OSes.

A faithful pure-Go port of Ruby's `Set`, cgo disabled, so it cross-compiles and embeds anywhere. Validated differentially against the system ruby binary, byte-for-byte. It is a standalone, reusable module extracted from rbgo's internals, and the backend bound by rbgo into the sibling org github.com/go-embedded-ruby.