diff --git a/activesupport/lib/active_support/syntax_error_proxy.rb b/activesupport/lib/active_support/syntax_error_proxy.rb index 59d96d86813b..a0e2ce385744 100644 --- a/activesupport/lib/active_support/syntax_error_proxy.rb +++ b/activesupport/lib/active_support/syntax_error_proxy.rb @@ -43,7 +43,8 @@ def backtrace_locations private def parse_message_for_trace - if __getobj__.to_s.start_with?("(eval") + # If RUBY_VERSION < 3.2, the path method is not available for SyntaxError + if is_eval? # If the exception is coming from a call to eval, we need to keep # the path of the file in which eval was called to ensure we can # return the right source fragment to show the location of the @@ -54,5 +55,15 @@ def parse_message_for_trace __getobj__.to_s.split("\n") end end + + if SyntaxError.method_defined?(:path) # Ruby 3.3+ + def is_eval? + __getobj__.path.start_with?("(eval") + end + else # 3.2 and older versions of Ruby + def is_eval? + __getobj__.to_s.start_with?("(eval") + end + end end end