Balázs' blog

Ruby Internal Types

While working on PlainAPM’s object allocation tracing callback, I needed a way to determine classes of arbitrary objects returned by rb_tracearg_object.

The naive way, calling rb_obj_classname was segfaulting, though, so I decided to handle this type-by-type, and either avoid the problem, or narrow down the scope of the issue, to understand it better.

Here are some notes that might save fifteen minutes to a future me, or to someone who’s starting to look into which types of objects are there in Ruby:

The internal types are defined by the ruby_value_type enum in value_type.h. Follow the comments from there.

A seemingly exhaustive switch based on the BUILTIN_TYPE(obj), lives in the objspace extension’s objspace_dump.c. The same file also includes Ruby API functions for inspecting properties of those types, which is especially useful for debugging.

Some of these types - T_ARRAY, T_CLASS, etc, correspond to what you usually think of as a Ruby object. Then a good bet is to go and check the header files in the include/ruby/internal/core directory for a definition of the corresponding struct.

Some types like T_IMEMO (this one is for debugging) or T_MOVED (related to GC compaction) correspond to a struct without a RBasic member, so they won’t have a class.

Last but not least, Ruby also has a T_ZOMBIE type, backed by a struct called RZombie - a decent punk rock band name if you ask me.

And, yes, if you were wondering, there is a make_zombie function… You are welcome :)