ruby / psych

A libyaml wrapper for Ruby
MIT License
566 stars 206 forks source link

Singleton Classes Aren't round-trippable #656

Open ccutrer opened 1 year ago

ccutrer commented 1 year ago
require "psych"
require "singleton"

class A
  include Singleton
end

YAML.unsafe_load(A.instance.to_yaml

result:

/Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:408:in `revive': private method `allocate' called for A:Class (NoMethodError)

        s = register(node, klass.allocate)
                                ^^^^^^^^^
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:215:in `visit_Psych_Nodes_Mapping'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:30:in `visit'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:6:in `accept'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:35:in `accept'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:320:in `visit_Psych_Nodes_Document'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:30:in `visit'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:6:in `accept'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:35:in `accept'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/nodes/node.rb:50:in `to_ruby'
    from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych.rb:274:in `unsafe_load'

I would expect a Singleton class to both a) not actually serialize any instance variables, and b) upon deserialization, just use the existing instance. Singleton does this when using Marshal, but Marshal is part of Ruby core. And Psych does not provide any way for a class to control allocation when coming from YAML the way that Marshal can, so even if the argument is that Singleton should control its own destiny (by implementing encode_with), Psych needs to provide a way to customize object allocation (without having to override allocate, which might make things complicated for allocating objects "normally").