Skip to content

Commit e0e6038

Browse files
committed
Fix raise signature for Ruby < 3.2
In Ruby versions older than 3.2, objects that respond to `#to_hash` are always used to extract keyword arguments from when `**kwargs` is present in a method signature. It causes these objects to be passed in as a hash to the `raise` method instead of an object. Code like `raise CustomError, object_implementing_to_hash`, which relies on `object_implementing_to_hash` to be an object, no longer works. In this commit we ensure above code keeps working until keyword arguments are fixed in Ruby 3.2.
1 parent e7d88f5 commit e0e6038

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/spring/application.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,26 @@ def shush_backtraces
302302
Kernel.module_eval do
303303
old_raise = Kernel.method(:raise)
304304
remove_method :raise
305-
define_method :raise do |*args, **kwargs|
306-
begin
307-
old_raise.call(*args, **kwargs)
308-
ensure
309-
if $!
310-
lib = File.expand_path("..", __FILE__)
311-
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
305+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
306+
define_method :raise do |*args, **kwargs|
307+
begin
308+
old_raise.call(*args, **kwargs)
309+
ensure
310+
if $!
311+
lib = File.expand_path("..", __FILE__)
312+
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
313+
end
314+
end
315+
end
316+
else
317+
define_method :raise do |*args|
318+
begin
319+
old_raise.call(*args)
320+
ensure
321+
if $!
322+
lib = File.expand_path("..", __FILE__)
323+
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
324+
end
312325
end
313326
end
314327
end

0 commit comments

Comments
 (0)