Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton Classes Aren't round-trippable #656

Open
ccutrer opened this issue Oct 12, 2023 · 0 comments
Open

Singleton Classes Aren't round-trippable #656

ccutrer opened this issue Oct 12, 2023 · 0 comments

Comments

@ccutrer
Copy link
Contributor

ccutrer commented Oct 12, 2023

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").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant