diff --git a/lib/method_source.rb b/lib/method_source.rb index ffd79ad..8819efa 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -152,7 +152,7 @@ def class_comment class_inst_or_module = class_inst_or_module.class end - location = class_inst_or_module.const_source_location(const_name) + location = class_inst_or_module.const_source_location("::#{const_name}") MethodSource.comment_helper(location, defined?(name) ? name : inspect) end diff --git a/spec/method_source_spec.rb b/spec/method_source_spec.rb index 1927670..67898bf 100644 --- a/spec/method_source_spec.rb +++ b/spec/method_source_spec.rb @@ -1,5 +1,11 @@ require 'spec_helper' +# Documentation for the outer class. +class MethodSourceShadowedClass + MethodSourceShadowedClass = Module.new + def work; end +end + describe MethodSource do describe "source_location (testing 1.8 implementation)" do @@ -96,6 +102,11 @@ # if RUBY_VERSION =~ /1.9/ || is_rbx? describe "Lambdas and Procs" do + it "uses the absolute class name when a nested constant shadows it" do + comment = MethodSourceShadowedClass.new.method(:work).class_comment + expect(comment).to eq("# Documentation for the outer class.\n") + end + it 'should return source for proc' do expect(MyProc.source).to eq(@proc_source) end