From 4999ad5e05f394f46d63ba77b5a216a20f177946 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Mon, 31 Aug 2026 00:49:14 +0200 Subject: [PATCH] Raise a source error for anonymous classes --- lib/method_source.rb | 5 ++++- spec/method_source_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/method_source.rb b/lib/method_source.rb index ffd79ad..d6fa7fa 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -152,6 +152,10 @@ def class_comment class_inst_or_module = class_inst_or_module.class end + unless const_name + raise SourceNotFoundError, "Could not locate source for an anonymous class or module!" + end + location = class_inst_or_module.const_source_location(const_name) MethodSource.comment_helper(location, defined?(name) ? name : inspect) @@ -174,4 +178,3 @@ class Proc include MethodSource::SourceLocation::ProcExtensions include MethodSource::MethodExtensions end - diff --git a/spec/method_source_spec.rb b/spec/method_source_spec.rb index 1927670..030f61f 100644 --- a/spec/method_source_spec.rb +++ b/spec/method_source_spec.rb @@ -123,6 +123,16 @@ it 'should return comment for class instance' do expect(C.new.method(:hello).class_comment).to eq(@class_comment) end + + it 'should raise a source error for an anonymous class' do + anonymous_class = Class.new do + def hello; end + end + + expect do + anonymous_class.instance_method(:hello).class_comment + end.to raise_error(MethodSource::SourceNotFoundError, /anonymous class/) + end end # end describe "Comment tests" do