summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2026-01-29 20:39:23 +0100
committergit <svn-admin@ruby-lang.org>2026-01-30 18:46:00 +0000
commitd7553015cc63258919f28b41c642eca66d35f8bf (patch)
treec95d4d469fff235f3513e941cf520d138408930c
parent1298f9ac1ad390594815bc4d3739eb312bca8887 (diff)
[ruby/prism] Handle String-like objects for Ripper.sexp
* RSpec relies on this in https://github.com/rspec/rspec/blob/rspec-support-v3.13.6/rspec-support/lib/rspec/support/source.rb#L57 which is given an RSpec::Support::EncodedString. * CI failure caused by this on truffleruby: https://github.com/sporkmonger/addressable/actions/runs/21457707372/job/61802608154#step:7:14 https://github.com/ruby/prism/commit/a83cb7b350
-rw-r--r--lib/prism/translation/ripper.rb12
-rw-r--r--test/prism/ruby/ripper_test.rb30
2 files changed, 41 insertions, 1 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb
index 054ad88ce3..5b2aa37833 100644
--- a/lib/prism/translation/ripper.rb
+++ b/lib/prism/translation/ripper.rb
@@ -480,7 +480,17 @@ module Prism
# Create a new Translation::Ripper object with the given source.
def initialize(source, filename = "(ripper)", lineno = 1)
- @source = source
+ if source.is_a?(IO)
+ @source = source.read
+ elsif source.respond_to?(:gets)
+ @source = +""
+ while line = source.gets
+ @source << line
+ end
+ else
+ @source = source.to_str
+ end
+
@filename = filename
@lineno = lineno
@column = 0
diff --git a/test/prism/ruby/ripper_test.rb b/test/prism/ruby/ripper_test.rb
index a89a9503b9..52a5ad7ef4 100644
--- a/test/prism/ruby/ripper_test.rb
+++ b/test/prism/ruby/ripper_test.rb
@@ -145,6 +145,36 @@ module Prism
assert_equal(Ripper.tokenize(source), Translation::Ripper.tokenize(source))
end
+ def test_sexp_coercion
+ string_like = Object.new
+ def string_like.to_str
+ "a"
+ end
+ assert_equal Ripper.sexp(string_like), Translation::Ripper.sexp(string_like)
+
+ File.open(__FILE__) do |file1|
+ File.open(__FILE__) do |file2|
+ assert_equal Ripper.sexp(file1), Translation::Ripper.sexp(file2)
+ end
+ end
+
+ File.open(__FILE__) do |file1|
+ File.open(__FILE__) do |file2|
+ object1_with_gets = Object.new
+ object1_with_gets.define_singleton_method(:gets) do
+ file1.gets
+ end
+
+ object2_with_gets = Object.new
+ object2_with_gets.define_singleton_method(:gets) do
+ file2.gets
+ end
+
+ assert_equal Ripper.sexp(object1_with_gets), Translation::Ripper.sexp(object2_with_gets)
+ end
+ end
+ end
+
# Check that the hardcoded values don't change without us noticing.
def test_internals
actual = Translation::Ripper.constants.select { |name| name.start_with?("EXPR_") }.sort