@@ -69,10 +69,18 @@ def initialize(irb_context)
69
69
70
70
def find_source ( signature , super_level = 0 )
71
71
case signature
72
- when /\A (::)?[A-Z]\w *(::[A-Z]\w *)*\z / # Const::Name
72
+ when /\A (::)?[A-Z]\w *(::[A-Z]\w *)*\z / # ConstName, ::ConstName, ConstPath::ConstName
73
73
eval_receiver_or_owner ( signature ) # trigger autoload
74
- base = @irb_context . workspace . binding . receiver . yield_self { |r | r . is_a? ( Module ) ? r : Object }
75
- file , line = base . const_source_location ( signature )
74
+ *parts , name = signature . split ( '::' , -1 )
75
+ base =
76
+ if parts . empty? # ConstName
77
+ find_const_owner ( name )
78
+ elsif parts == [ '' ] # ::ConstName
79
+ Object
80
+ else # ConstPath::ConstName
81
+ eval_receiver_or_owner ( parts . join ( '::' ) )
82
+ end
83
+ file , line = base . const_source_location ( name )
76
84
when /\A (?<owner>[A-Z]\w *(::[A-Z]\w *)*)#(?<method>[^ :.]+)\z / # Class#method
77
85
owner = eval_receiver_or_owner ( Regexp . last_match [ :owner ] )
78
86
method = Regexp . last_match [ :method ]
@@ -122,5 +130,10 @@ def eval_receiver_or_owner(code)
122
130
rescue NameError
123
131
raise EvaluationError
124
132
end
133
+
134
+ def find_const_owner ( name )
135
+ module_nesting = @irb_context . workspace . binding . eval ( '::Module.nesting' )
136
+ module_nesting . find { |mod | mod . const_defined? ( name , false ) } || module_nesting . find { |mod | mod . const_defined? ( name ) } || Object
137
+ end
125
138
end
126
139
end
0 commit comments