go-ruby-abbrev

Ruby's abbrev โ€” unambiguous prefix abbreviations โ€” in pure Go, MRI-compatible, no cgo.

pure Go ยท zero cgo Abbrev.abbrev Array#abbrev unambiguous prefixes literal prefix filter multibyte chars empty/dup words Akinori MUSHA port MRI byte-exact 100% coverage 6 arches ยท 3 OSes
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-abbrev is a pure-Go (no cgo) reimplementation of Ruby's abbrev standard library โ€” MRI's Abbrev.abbrev and the Array#abbrev core extension. Given a set of words it computes the set of unambiguous abbreviations: every prefix that identifies exactly one word, plus each full word. It is a faithful, byte-for-byte port of upstream abbrev.rb (Akinori MUSHA), reproducing MRI's edge cases โ€” ambiguous prefixes dropped, empty/duplicate words, a literal (not pattern) prefix filter, and multibyte words split on characters โ€” without any Ruby runtime. It is bound into go-embedded-ruby by rbgo as a native module just like go-ruby-regexp and go-ruby-erb โ€” differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Unambiguous abbreviations ready

Given a set of words, every prefix that identifies exactly one word plus each full word; a prefix shared by two or more words never appears, and each full word always maps to itself.

Optional prefix filter ready

Abbrev(words, prefix) keeps only words starting with prefix. The prefix is a literal string, not a pattern โ€” MRI anchors it as /\A<quoted>/ โ€” so Abbrev(["a.b", "axb"], "a.") matches only "a.b".

Both Ruby entry points ready

The idiomatic Go shape of both Abbrev.abbrev(words[, prefix]) and the Array#abbrev core extension (words.abbrev), collapsed into a single Abbrev(words, prefix...) with a variadic optional prefix.

MRI edge cases ready

Empty words contribute no prefixes but still map to themselves; duplicate words collapse; multibyte words split on characters, not bytes, matching Ruby’s String#[] โ€” so Abbrev(["cafรฉ", "cane"]) includes "caf".

Differential oracle & coverage ready

Deterministic, runtime-free cases drive coverage to 100%, plus a differential oracle that runs the same inputs through the live ruby binary (Abbrev.abbrev) and asserts byte-identical maps; gofmt + go vet clean, green across six 64-bit arches and three OSes.

A faithful port of Ruby's abbrev standard library in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. Given a word list it computes every unambiguous prefix plus each full word, reproducing MRI's edge cases โ€” ambiguous prefixes dropped, empty and duplicate words, a literal-string (not pattern) prefix filter, and multibyte words split on characters. Validated differentially against the system ruby binary (Abbrev.abbrev). It is a standalone, reusable module bound into the sibling org github.com/go-embedded-ruby.