Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Singleton Classes Aren't round-trippable

Đang mở
#656 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
ruby
Lĩnh vực
tooling

Hướng nghiên cứu

Tái hiện ví dụ Singleton bằng YAML.unsafe_load, sau đó đọc psych/visitors/to_ruby.rb xung quanh revive và lệnh gọi klass.allocate được báo cáo. So sánh hành vi với cách Singleton xử lý Marshal và xác định việc tùy chỉnh cấp phát nên hoạt động như thế nào. Hoàn tất khi các instance của Singleton round-trip mà không tuần tự hóa các biến instance và quá trình giải tuần tự hóa sử dụng lại instance hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Ruby
Star
597
Fork
223
Merge trung bình
6 giờ 8 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ruby/psych

Tất cả issue của ruby/psych

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.