Project

General

Profile

Bug #16506

Documentation for `Module#const_souce_location` is wrong

Added by sawa (Tsuyoshi Sawada) about 1 month ago. Updated 28 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:96809]

Description

https://ruby-doc.org/core-2.7.0/Module.html#method-i-const_source_location says:

Returns the Ruby source filename and line number containing first definition of constant specified.

It should be:

Returns the Ruby source filename and line number containing the last definition (the effective definition) of the constant specified.

It also has an example line:

p Object.const_source_location('A')       # => ["test.rb", 1]  -- note it is first entry, not "continuation"

but that may give the impression that the first-ness is due to the nature of this method. The reason ["test.rb", 1] is returned instead of ["test.rb", 14] is because the constant is created at line 1, and is only reopened/modified in line 14. Perhaps, this line can be changed to:

p Object.const_source_location('A')       # => ["test.rb", 1]  -- note 'A' is created in line 1, and is only reopened/modified in "continuation"

Adding something like this may make it clear that it is the last definition that is returned:

D = 'D1'
D = 'D2'
D = 'D3'

...

Object.const_source_location('D') # => returns the location that corresponds to 'D3'
#1

Updated by sawa (Tsuyoshi Sawada) about 1 month ago

  • Description updated (diff)

Updated by zverok (Victor Shepelev) about 1 month ago

sawa (Tsuyoshi Sawada) That's an interesting question! I wrote an original doc, and I meant this:

# test.rb:
class A
end

class A # reopening
end

Object.const_source_location('A') # => ["test.rb", 1] -- first definition

But of course, if we are talking about plain constants (not classes), you are right:

# test.rb:
A = 1

A = 2

Object.const_source_location('A') # => ["test.rb", 3] -- LAST definition

I'll submit a PR with clarifications to cover both cases.

Updated by zverok (Victor Shepelev) about 1 month ago

Ah, while I was writing the comment, you've updated the description with the same thoughts :)
Will fix.

Updated by jeremyevans0 (Jeremy Evans) 28 days ago

  • Status changed from Open to Closed

Also available in: Atom PDF