Project

General

Profile

Feature #10771

An easy way to get the source location of a constant

Added by sawa (Tsuyoshi Sawada) about 4 years ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:67745]

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 to Method class) and a constant method (similar to method 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

const_source_location.tar.gz (1.96 KB) const_source_location.tar.gz Patches against Ruby trunk jphelps (Jeremy Phelps), 06/26/2017 01:31 AM

History

Updated by ko1 (Koichi Sasada) about 4 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

Matz issue.

Updated by jphelps (Jeremy Phelps) over 1 year 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 1 year 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 inputs in web browsers and
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 1 year 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 1 year 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 1 year 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) 4 months 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) 4 months 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) 3 months ago

We picked constant_source_location. We will experiment with this method in 2.7 development.

Matz.

Also available in: Atom PDF