# Ruby Instance Variables Are Not Inherited (and Why That Breaks Your DSL)

DevFeed: [Ruby Instance Variables Are Not Inherited (and Why That Breaks Your DSL)](<https://devfeed.tech/articles/ruby-instance-variables-are-not-inherited-and-why-that-breaks-your-dsl-20532.md>)

Original publisher: [Read original article](<https://code.dblock.org/2026/08/14/instance-variables-are-not-inherited-and-why-that-breaks-your-dsl.html>)

Author: Daniel Doubrovkine (dblock@dblock.org)

Published: 2026-08-14T00:00:00Z

Content type: article

Language: en

Sources: [Daniel Doubrovkine](<https://devfeed.tech/sources/daniel-doubrovkine.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [bug](<https://devfeed.tech/topics/bug.md>), [Library](<https://devfeed.tech/topics/library.md>), [Development](<https://devfeed.tech/topics/development.md>), [Rails](<https://devfeed.tech/topics/rails.md>)

Tags: [bug](<https://devfeed.tech/tags/bug.md>), [development](<https://devfeed.tech/tags/development.md>), [library](<https://devfeed.tech/tags/library.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [pull-requests](<https://devfeed.tech/tags/pull-requests.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

This article explains how Ruby instance variables used by the ruby-enum gem are not inherited by subclasses, causing failures involving subclassing, class reloading, and redundant module inclusion. It describes fixes shipped in ruby-enum 1.2.0, including lazy defaults and explicit merging of inherited enums.

## Source excerpt

The ruby-enum gem is a small library I maintain that adds enum-like behavior to a class via include Ruby::Enum and define :KEY, value. Four pull requests landed against it recently, each fixing a different symptom, and all four turned out to be the same underlying bug: class-level instance variables set in a module's included hook are not inherited by subclasses the way you might expect. All of these fixes shipped in ruby-enum 1.2.0. Ruby::Enum stores its keys and values in instance variables on the class itself, set up when the module is included. def self.included(base) base.extend ClassMethods base.instance_variable_set(:@_enum_hash, {}) base.instance_variable_set(:@_enums_by_value, {}) end This works fine for a single class. It gets interesting the moment subclasses or class reloading show up. A user opened #56, reporting DuplicateKeyError from a Rails console after a class that had already defined its enums got reloaded. It reproduces without Rails. class_body = proc do include Ruby::Enum define :RED, 'red' end Colors = Class.new(&class_body) Colors.class_eval(&class_body) # raises DuplicateKeyError Rails' development-mode autoloader re-evaluates a class body, and the second define :RED sees @_enum_hash still holding RED from the first load, so the duplicate-key check fires. The fix clears the hash whenever Ruby::Enum is included, rather than assuming it's always a fresh class. The contributor that reported the problem and submitted the fix also caught a sneakier variant: a subclass that redundantly re-includes Ruby::Enum (include is supposed to be idempotent, right?) was wiping out enums inherited from its parent, because included unconditionally reset the instance variables: class OtherSecondSubclass < FirstSubclass include Ruby::Enum # redundant, but shouldn't lose FirstSubclass's enums define :MAGENTA, 'magenta' end Thanks, Nazar, for tracking both of these down with tests. Months later, #49 surfaced a related crash: a subclass that never calls define itsel