ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: dylan.smith@shopify.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103027] [Ruby master Feature#17749] Const source location without name
Date: Fri, 26 Mar 2021 00:12:21 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91091.20210326001220.73@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17749.20210325232948.73@ruby-lang.org

Issue #17749 has been updated by dylants (Dylan Thacker-Smith).


I agree that `source_location` would be better for this new feature, since it is referring to the receiver of the call.  `const_source_location` makes sense for the existing method, since it is referring to the source location of something else, a nested constant.  So I think you should make a new `source_location` method rather than trying to combine them.

----------------------------------------
Feature #17749: Const source location without name
https://bugs.ruby-lang.org/issues/17749#change-91091

* Author: tenderlovemaking (Aaron Patterson)
* Status: Open
* Priority: Normal
----------------------------------------
Hi,

I would like to be able to ask a class or module what its source location is without knowing the name.  For example, I want to do this:

```ruby
module A
  class B
  end
end

p A::B.const_source_location
```

In other works `A::B.const_source_location` would be equivalent to `A.const_source_location(:B)`.

The reason I want to do this is because sometimes it is very difficult to get the name of a constant, and sometimes I don't have access to the constant that "encloses" the class or module.

One example:

```ruby
ObjectSpace.each_object(Class) do |k|
  p k.const_source_location
end
```

In this case I have class objects, but I can't tell what constant `k` was defined *inside* of.  Also I can't trust the "name" method on `k` because sometimes it's not the default method (of course I could work around that, but it's not fun).

I've attached a patch that implements the feature, and there is a PR [here](https://github.com/ruby/ruby/pull/4324)

Side note: I think I would like "source_location" better than `const_source_location`, but I wanted to just file a feature request so we could talk about the feature in general.

---Files--------------------------------
0001-Add-constant-location-information-to-classes.patch (5.34 KB)


-- 
https://bugs.ruby-lang.org/

  reply	other threads:[~2021-03-26  0:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-25 23:29 [ruby-core:103026] [Ruby master Feature#17749] Const source location without name tenderlove
2021-03-26  0:12 ` dylan.smith [this message]
2021-03-26  2:01 ` [ruby-core:103028] " shevegen
2021-03-26  6:57 ` [ruby-core:103034] " sawadatsuyoshi
2021-03-26 16:09 ` [ruby-core:103039] " tenderlove
2021-03-26 17:44 ` [ruby-core:103041] " eregontp
2021-03-26 17:45 ` [ruby-core:103042] " eregontp
2021-04-16  6:21 ` [ruby-core:103473] " mame
2021-04-16 18:00 ` [ruby-core:103484] " tenderlove
2021-04-17  7:38 ` [ruby-core:103494] " mame

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-91091.20210326001220.73@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).