Skip to content

Commit 0f40f56

Browse files
committedMay 12, 2025
Fix dumping StringIO (and potentially others) on Ruby <= 2.7
In Ruby < 3.0, the superclass of StringIO was actually already `Data`, but it doesn't have the expected shape. So, on these earlier versions it errors: > NoMethodError: undefined method `members' for #<StringIO:0x00005641dd5f2880> > vendor/bundle/ruby/2.6.0/gems/psych-5.2.5/lib/psych/visitors/yaml_tree.rb:170:in `visit_Data' This test doesn't fail on 2.7, presumably because it can pull in a newer `stringio` version.
1 parent dbf9e36 commit 0f40f56

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
 

‎lib/psych/visitors/yaml_tree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def visit_Data o
198198

199199
@emitter.end_mapping
200200
end
201-
end
201+
end unless RUBY_VERSION < "3.2"
202202

203203
def visit_Struct o
204204
tag = ['!ruby/struct', o.class.name].compact.join(':')

‎test/psych/test_stringio.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
require_relative 'helper'
3+
4+
module Psych
5+
class TestStringIO < TestCase
6+
# The superclass of StringIO before Ruby 3.0 was `Data`,
7+
# which can interfere with the Ruby 3.2+ `Data` dumping.
8+
def test_stringio
9+
assert_nothing_raised do
10+
Psych.dump(StringIO.new("foo"))
11+
end
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)