mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add FrozenError#receiver
Similar to NameError#receiver, this returns the object on which the modification was attempted. This is useful as it can pinpoint exactly what is frozen. In many cases when a FrozenError is raised, you cannot determine from the context which object is frozen that you attempted to modify. Users of the current rb_error_frozen C function will have to switch to using rb_error_frozen_object or the new rb_frozen_error_raise in order to set the receiver of the FrozenError. To allow the receiver to be set from Ruby, support an optional second argument to FrozenError#initialize. Implements [Feature #15751]
This commit is contained in:
parent
897901283c
commit
39eadca76b
7 changed files with 121 additions and 7 deletions
34
spec/ruby/core/exception/frozen_error_spec.rb
Normal file
34
spec/ruby/core/exception/frozen_error_spec.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
require_relative '../../spec_helper'
|
||||
|
||||
describe "FrozenError" do
|
||||
ruby_version_is "2.5" do
|
||||
it "is a subclass of RuntimeError" do
|
||||
RuntimeError.should be_ancestor_of(FrozenError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "FrozenError.new" do
|
||||
ruby_version_is "2.7" do
|
||||
it "should take optional receiver argument" do
|
||||
o = Object.new
|
||||
FrozenError.new("msg", o).receiver.should equal(o)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "FrozenError#receiver" do
|
||||
ruby_version_is "2.7" do
|
||||
it "should return frozen object that modification was attempted on" do
|
||||
o = Object.new.freeze
|
||||
begin
|
||||
def o.x; end
|
||||
rescue => e
|
||||
e.should be_kind_of(FrozenError)
|
||||
e.receiver.should equal(o)
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue