Skip to content

Commit

Permalink
Fix NoMethodError#message when receiver is an instance of an anonymou…
Browse files Browse the repository at this point in the history
…s class
  • Loading branch information
andrykonchin committed Dec 19, 2024
1 parent 4cdd52d commit 4fe8f1c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
11 changes: 11 additions & 0 deletions spec/ruby/core/exception/no_method_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ def inspect
end
end

it "uses class' string representation when receiver is a singleton class" do
obj = Object.new
singleton_class = obj.singleton_class

begin
singleton_class.foo
rescue NoMethodError => error
error.message.should =~ /\Aundefined method [`']foo' for class #<Class:#<Object:0x\h+>>\Z/
end
end

it "uses #name when receiver is a module" do
begin
NoMethodErrorSpecs::AModule.foo
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/exception/no_method_error_tags.txt

This file was deleted.

11 changes: 4 additions & 7 deletions src/main/ruby/truffleruby/core/truffle/exception_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,15 @@ def self.receiver_string(receiver)
when NilClass, TrueClass, FalseClass
Truffle::Type.rb_inspect(receiver)
when Class
unless receiver.singleton_class?
"class #{receiver}"
end
# otherwise fall through to rb_any_to_s
"class #{receiver}"
when Module
"module #{receiver}"
else
klass = Primitive.metaclass(receiver)
if klass.name
"an instance of #{klass.name}"
unless klass.singleton_class?
"an instance of #{klass}"
end
# else fall through to rb_any_to_s
# otherwise fall through to rb_any_to_s
end
rescue Exception # rubocop:disable Lint/RescueException
nil
Expand Down

0 comments on commit 4fe8f1c

Please sign in to comment.