From: "austin (Austin Ziegler)" Date: 2022-07-30T18:59:16+00:00 Subject: [ruby-core:109385] [Ruby master Feature#18930] Officially deprecate class variables Issue #18930 has been updated by austin (Austin Ziegler). If we���re going to deprecate them, we need something that effectively replaces them���and class instance variables aren���t it. I was experimenting with something along the lines of: ```ruby class Base def self.dependents @dependents ||= {} end def self.inherited(klass) return unless klass.name.match(/V(\d+)\z/) name = $1.to_i dependents[name] = klass end end class Rand < Base end class Timestamp < Base end class V1 < Rand end class V2 < Timestamp end class V3 < Timestamp end class V4 < Timestamp end ``` This results in `Base.dependents` producing `{}`, but `Rand.dependents` producing `{1=>V1}` and `Timestamp.dependents` producing `{2=>V2,3=>V3,4=>V4}`. Switching to a class variable produces the result expected. I���ve managed to do this a different way, but automatic collection of all dependents in a hierarchy only at the top level requires a bit of a hacky workaround (`Base.dependents[name] = klass`). Yes, in this case, I could *probably* switch to a constant (even a private constant), but that also feels wrong. ---------------------------------------- Feature #18930: Officially deprecate class variables https://bugs.ruby-lang.org/issues/18930#change-98535 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal ---------------------------------------- Ruby's class variables are very confusing, and it seem many people agree they should be avoided (#18927). How about we deprecate them officially? Concretely: * Mention in the documentation that class variables are deprecated and should be avoided/should not be used. * Add a parse-time deprecation warning, now that we only see those with `Warning[:deprecation] = true` it seems reasonable to add. -- https://bugs.ruby-lang.org/ Unsubscribe: