go-ruby-ostruct

Ruby's OpenStruct data core in pure Go โ€” MRI-compatible, no cgo.

pure Go ยท zero cgo OpenStruct ordered attribute table [] / []= to_h ยท each_pair ยท dig delete_field == / eql? MRI-exact inspect MRI byte-exact 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-ostruct is a pure-Go (no cgo) reimplementation of the data structure underneath Ruby's OpenStruct โ€” an ordered, Symbol-keyed attribute table with the accessors, conversions, comparison, and inspection MRI exposes: [] / []=, to_h, each_pair, dig, delete_field, == / eql?, and inspect / to_s. It matches reference Ruby byte-for-byte on the inspect format, to_h insertion ordering, and delete_field semantics. OpenStruct's dynamic accessors โ€” turning any method name into a read or write via method_missing / respond_to_missing? โ€” stay in the host (rbgo) and are implemented in terms of this table. It was extracted from rbgo's internals into a reusable standalone library: no dependency on the Ruby runtime, the dependency runs the other way. It is the OpenStruct backend for go-embedded-ruby, bound 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.

Ordered attribute table ready

The data structure underneath OpenStruct: an ordered, Symbol-keyed attribute table built from New(pairs...) (OpenStruct.new(hash)), preserving insertion order exactly as MRI’s to_h does.

Accessors ready

Get / Set โ€” the targets a reader and a name= writer dispatch to (method_missing) โ€” plus Index / SetIndex for [] / []=, and RespondToField, the table half of respond_to_missing?.

Conversions & traversal ready

ToH (Symbol keys, insertion order), EachPair, Members, and Dig(keys...) routed through a Digger interface so the host’s Ruby values dig exactly as in MRI.

delete_field & comparison ready

DeleteField returns the old value and raises NameError when absent, and Equal / Eql implement == / eql? โ€” all matching MRI’s semantics.

MRI-exact inspect ready

Inspect / String render #<OpenStruct name="John", age=70> byte-for-byte, through an Inspector interface with a built-in renderer for the common scalar/collection shapes, for deterministic ruby-free testing.

Differential oracle & coverage ready

The inspect format, to_h insertion ordering, and delete_field semantics compared against the system ruby byte-for-byte; 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.

A faithful port of the OpenStruct data structure in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It owns the ordered, Symbol-keyed attribute table and its MRI-faithful accessors, conversions, comparison, and inspect; the dynamic attribute dispatch stays in the host, implemented in terms of this table. Validated differentially against the system ruby binary โ€” the inspect format, to_h ordering, and delete_field semantics compared byte-for-byte. It is a standalone, reusable module extracted from rbgo's internals, and the OpenStruct backend for the sibling org github.com/go-embedded-ruby.