Skip to content

Commit

Permalink
Fix raise signature for Ruby < 3.2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
esposito committed Aug 2, 2023
1 parent e7d88f5 commit e0e6038
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/spring/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,26 @@ def shush_backtraces
Kernel.module_eval do
old_raise = Kernel.method(:raise)
remove_method :raise
define_method :raise do |*args, **kwargs|
begin
old_raise.call(*args, **kwargs)
ensure
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
define_method :raise do |*args, **kwargs|
begin
old_raise.call(*args, **kwargs)
ensure
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
end
end
end
else
define_method :raise do |*args|
begin
old_raise.call(*args)
ensure
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
end
end
end
end
Expand Down

0 comments on commit e0e6038

Please sign in to comment.