Feature #10771
closedAn easy way to get the source location of a constant
Description
For constants, it is difficult to get the source location where it was (last) defined. I request either of the following to be implemented:
-
Tracepoint emits a signal when a constant is defined.
-
Implement a
Constant
class (similar toMethod
class) and aconstant
method (similar tomethod
method) that behave as follows:foo1.rb 1| module Foo 2| Bar = :bar 3| end 4| Foo.constant(:Bar) #=> #<Constant: Foo#Bar> 5| Foo.constant(:Bar).source_location #=> ["foo1.rb", 2]
-
Implement
Module#constant_source_location
foo2.rb 1| module Foo 2| Bar = :bar 3| end 4| Foo.constant_source_location(:Bar) #=> ["foo2.rb", 2]
Files
Updated by ko1 (Koichi Sasada) almost 10 years ago
- Assignee set to matz (Yukihiro Matsumoto)
Matz issue.
Updated by jphelps (Jeremy Phelps) over 7 years ago
I've got a patch ready for this, but I have no idea how to submit it. There are serious contradictions in this page:
https://bugs.ruby-lang.org/projects/ruby/wiki/HowToContribute
It says that the changes should be against "the trunk of Ruby", and that "only Ruby 2.4 may accept new features." But the trunk on GitHub is Ruby 2.5.
It says that the patch should be a unified diff, but it also says different changes should not be mixed in a single commit. But diff files don't have commits; only pull requests and whole repos do. But it says that you only accept PRs for "tiny fixes", and only if they don't require discussion.
Updated by normalperson (Eric Wong) over 7 years ago
jeremy.phelps@instacart.com wrote:
I've got a patch ready for this, but I have no idea how to
submit it. There are serious contradictions in this page:https://bugs.ruby-lang.org/projects/ruby/wiki/HowToContribute
It says that the changes should be against "the trunk of
Ruby", and that "only Ruby 2.4 may accept new features." But
the trunk on GitHub is Ruby 2.5.
Make the change against trunk, any mentions of specific version
numbers is probably outdated. Maybe you have permissions to edit
that page? (I can't stand
reply to redmine via email)
It says that the patch should be a unified diff, but it also
says different changes should not be mixed in a single commit.
But diff files don't have commits; only pull requests and
whole repos do. But it says that you only accept PRs for "tiny
fixes", and only if they don't require discussion.
The output of "git format-patch" is a superset of unified diff,
you should use that to generate diffs if using git. "git am"
can be used to apply that output and preserve authorship info
and the commit message.
You can also publish your work to any publically accessible git
repository tell us about how to pull it, here. The
"git request-pull" command can generate the message text for you.
Updated by jphelps (Jeremy Phelps) over 7 years ago
Ok, then here's my patch. I called the method "const_source_location" instead of "constant_source_location", because it uses a lot of the same code as "const_get".
I don't have access to edit the wiki page.
Updated by normalperson (Eric Wong) over 7 years ago
jeremy.phelps@instacart.com wrote:
Ok, then here's my patch. I called the method
"const_source_location" instead of "constant_source_location",
because it uses a lot of the same code as "const_get".
Thanks. The code looks fine.
I'd suggest rb_ary_new_from_args to avoid the temporary array
(instead of rb_ary_new4) but we can fix it at commit time.
It's up to matz now to approve/deny the feature itself.
Thanks again
Updated by duerst (Martin Dürst) over 7 years ago
jphelps (Jeremy Phelps) wrote:
I've got a patch ready for this, but I have no idea how to submit it. There are serious contradictions in this page:
https://bugs.ruby-lang.org/projects/ruby/wiki/HowToContribute
I updated this page a bit, hopefully removing the contradictions. Please tell us if there still are some problems.
Updated by shevegen (Robert A. Heiler) almost 6 years ago
janfri suggested this issue for the next upcoming developer meeting
at December 2018 so I think it will be approved or rejected then.
I did not yet comment on it so I will comment on it briefly.
- I think the functionality would be nice to have; introspection
is very useful, IMO.
Not sure how matz feels about the API suggested but the functionality
may be ok?
Updated by sawa (Tsuyoshi Sawada) almost 6 years ago
I am glad that this issue is going to be discussed in the next developer meeting, but now that we have a way to know not only the file name and the line number but also the column number/position as well for method definitions, I request that this be done for constants as well.
Updated by matz (Yukihiro Matsumoto) almost 6 years ago
We picked constant_source_location
. We will experiment with this method in 2.7 development.
Matz.
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Status changed from Open to Closed
Applied in changeset git|93843830198ba436e2ea21a60a11758d47cf521b.
Module#constant_source_location [Feature #10771]