diff options
author | Jean Boussier <[email protected]> | 2025-05-03 12:57:39 +0200 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2025-05-08 18:03:04 +0900 |
commit | 1f2a9dc2a912118e77ec9f01e9ad4002ccde52b5 (patch) | |
tree | 4a4defceb58af3fbb3beaf15e0d08d5402d93775 | |
parent | 49b4e0350d1a55f9372760b0b164039c7535b85e (diff) |
[ruby/psych] 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.
https://github.com/ruby/psych/commit/d58cff11af
-rw-r--r-- | ext/psych/lib/psych.rb | 3 | ||||
-rw-r--r-- | ext/psych/lib/psych/core_ext.rb | 15 |
2 files changed, 13 insertions, 5 deletions
diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index a95393c281..d7567d87e9 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -23,7 +23,6 @@ require_relative 'psych/parser' require_relative 'psych/omap' require_relative 'psych/set' require_relative 'psych/coder' -require_relative 'psych/core_ext' require_relative 'psych/stream' require_relative 'psych/json/tree_builder' require_relative 'psych/json/stream' @@ -761,3 +760,5 @@ module Psych self.domain_types = {} # :startdoc: end + +require_relative 'psych/core_ext' diff --git a/ext/psych/lib/psych/core_ext.rb b/ext/psych/lib/psych/core_ext.rb index 950b20f2d6..fc259fd4a2 100644 --- a/ext/psych/lib/psych/core_ext.rb +++ b/ext/psych/lib/psych/core_ext.rb @@ -18,12 +18,19 @@ if defined?(::IRB) require_relative 'y' end - -# TODO: how best to check for builtin Set? -if defined?(::Set) && Object.const_source_location(:Set) == ["ruby", 0] +# Up to Ruby 3.4, Set was a regular object and was dumped as such +# by Pysch. +# Starting from Ruby 3.5 it's a core class written in C, so we have to implement +# #encode_with / #init_with to preserve backward compatibility. +if defined?(::Set) && Set.new.instance_variables.empty? class Set def encode_with(coder) - coder["hash"] = to_h + hash = {} + each do |m| + hash[m] = true + end + coder["hash"] = hash + coder end def init_with(coder) |