@dhiemstra

RSS
Dec 5

Sharing static variables through the inheritance tree in Ruby

I’ve been struggling a couple of hours with trying to declare static variables inside subclasses that can be used in the baseclass. Unfortunately ruby shares the class variables across all subclasses, this can lead into some unexpected behavior, see the following example:

After a lot of googling I gladly came across class_attribute.

Declare a class-level attribute whose value is inheritable by subclasses.

And now the final result:

For more information and the full docs, see: apidock.com/rails/Class/class_attribute