Skip to content

Commit d58cff1

Browse files
committed
Refine Ruby 3.5 Set support.
Use feature testing to detect native Set, and don't rely on `Set#to_h` which wasn't intended as a public method.
1 parent 0b8f7ef commit d58cff1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/psych.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
require_relative 'psych/omap'
2424
require_relative 'psych/set'
2525
require_relative 'psych/coder'
26-
require_relative 'psych/core_ext'
2726
require_relative 'psych/stream'
2827
require_relative 'psych/json/tree_builder'
2928
require_relative 'psych/json/stream'
@@ -761,3 +760,5 @@ def domain_types=(value)
761760
self.domain_types = {}
762761
# :startdoc:
763762
end
763+
764+
require_relative 'psych/core_ext'

lib/psych/core_ext.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ def to_yaml options = {}
1818
require_relative 'y'
1919
end
2020

21-
22-
# TODO: how best to check for builtin Set?
23-
if defined?(::Set) && Object.const_source_location(:Set) == ["ruby", 0]
21+
# Up to Ruby 3.4, Set was a regular object and was dumped as such
22+
# by Pysch.
23+
# Starting from Ruby 3.5 it's a core class written in C, so we have to implement
24+
# #encode_with / #init_with to preserve backward compatibility.
25+
if defined?(::Set) && Set.new.instance_variables.empty?
2426
class Set
2527
def encode_with(coder)
26-
coder["hash"] = to_h
28+
hash = {}
29+
each do |m|
30+
hash[m] = true
31+
end
32+
coder["hash"] = hash
33+
coder
2734
end
2835

2936
def init_with(coder)

0 commit comments

Comments
 (0)