
Issue #19742 has been updated by ioquatix (Samuel Williams). I'll add my observation that Marshal states it cannot handle anonymous modules, but I say, it cannot handle ANY non-permanent (temporary) modules (assuming `permanent?` as defined above). The difference is subtle but demonstrated by this example: ```ruby # (1) Expected: point = Struct.new(:x, :y) Marshal.dump(point.new(1, 2)) # can't dump anonymous class #<Class:0x00000001084f14c0> (TypeError) # (2) Point2 is not anonymous (it did have a name), but it's also not permanent (the name is no longer valid). Point = Struct.new(:x, :y) point = Point Point2 = Point Object.send(:remove_const, :Point) Marshal.dump(Point2.new(1, 2)) # undefined class/module Point (ArgumentError) # (3) Try to trick Marshal: Point = Struct.new(:x, :y) Marshal.dump(point.new(1, 2)) # Point can't be referred to (TypeError) ``` In all the above examples, the marshalled class may or may not be anonymous, but it's always not permanent, according to my (slightly fixed) definition: ```ruby class Module def permanent? Object.const_get(self.name).equal?(self) rescue nil end end ``` If being "not anonymous" was the only criteria, re-assigning the constants would ---------------------------------------- Feature #19742: Introduce `Module#anonymous?` https://bugs.ruby-lang.org/issues/19742#change-103642 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- As a follow-on <from https://bugs.ruby-lang.org/issues/19521>, I'd like propose we introduce `Module#anonymous?`. In some situations, like logging/formatting, serialisation/deserialization, debugging or meta-programming, we might like to know if a class is a proper constant or not. However, this brings about some other issues which might need to be discussed. After assigning a constant, then removing it, the internal state of Ruby still believes that the class name is permanent, even thought it's no longer true. e.g. ``` m = Module.new m.anonymous? # true M = m m.anonyomous # false Object.send(:remove_const, :M) M # uninitialized constant M (NameError) m.anonymous? # false ``` Because RCLASS data structure is not updated after the constant is removed, internally the state still has a "permanent class name". I want to use this proposal to discuss this issue and whether there is anything we should do about such behaviour (or even if it's desirable). Proposed PR: https://github.com/ruby/ruby/pull/7966 cc @fxn -- https://bugs.ruby-lang.org/