Singleton Classes Aren't round-trippable
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 597
- Forks
- 223
- Avg merge
- 11h 23m
- Merged PRs (30d)
- 3
Description
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").
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the Singleton example with YAML.unsafe_load, then read psych/visitors/to_ruby.rb around revive and the reported klass.allocate call. Compare the behavior with Singleton's Marshal handling and determine how allocation customization should work. Done means singleton instances round-trip without serializing instance variables and deserialization reuses the existing instance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100