1 day ago · Tech · hide · 0 comments

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…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.