ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: fxn@hashref.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103382] [Ruby master Feature#17753] Add Module#namespace
Date: Sat, 10 Apr 2021 20:45:35 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91475.20210410204534.73@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17753.20210326191942.73@ruby-lang.org

Issue #17753 has been updated by fxn (Xavier Noria).


Also, in case my comments above are too genric, let's take the use case in the description of the ticket:

> I can do A::B::C.outer_scope.constants to find the list of "sibling" constants to C.

Let's consider

```ruby
module A
  module B
    class C; end
    class D; end
  end 
end

module X
  Y = A::B::C
  Z = 1
end
```

In what sense is `A::B::D` a sibling of `A::B::C` and `X::Z` is not?

If our input is a class object, as in the `ObjectSpace` example, you have no information that allows you to jump from it to its possibly multiple places in which the object may be stored. And the original constant may be gone, those places can be elsewhere (as it happens with stale class objects cached during Rails initialization after a reload).

On the other hand, if you are in a very specific situation where you can assume that loop makes sense for all `k`, you can always `name.sub(/::\w+$/, '')` and `const_get`, modulus details. In a project, in a library, you may have constraints in place that you can exploit. In Ruby, the language, you don't.


----------------------------------------
Feature #17753: Add Module#namespace
https://bugs.ruby-lang.org/issues/17753#change-91475

* Author: tenderlovemaking (Aaron Patterson)
* Status: Open
* Priority: Normal
----------------------------------------
Given code like this:

```ruby
module A
  module B
    class C; end
    class D; end
  end
end
```

We can get from `C` to `B` like `C.outer_scope`, or to `A` like
`C.outer_scope.outer_scope`.

I want to use this in cases where I don't know the outer scope, but I
want to find constants that are "siblings" of a constant.  For example,
I can do `A::B::C.outer_scope.constants` to find the list of "sibling"
constants to `C`.  I want to use this feature when walking objects and
introspecting.  For example:

```ruby
ObjectSpace.each_object(Class) do |k|
  p siblings: k.outer_scope.constants
end
```

I've attached a patch that implements this feature, and there is a pull request on GitHub [here](https://github.com/ruby/ruby/pull/4326).

---Files--------------------------------
0001-Add-Module-outer_scope.patch (5.93 KB)
0001-Add-Module-namespace.patch (5.89 KB)


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

  parent reply	other threads:[~2021-04-10 20:45 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-26 19:19 [ruby-core:103044] [Ruby master Feature#17753] Add Module#outer_scope tenderlove
2021-03-27  7:58 ` [ruby-core:103053] " sawadatsuyoshi
2021-03-27 11:33 ` [ruby-core:103057] " eregontp
2021-03-27 21:51 ` [ruby-core:103065] " tenderlove
2021-03-28  6:05 ` [ruby-core:103069] " sawadatsuyoshi
2021-03-28 13:33 ` [ruby-core:103072] " jean.boussier
2021-04-10 10:35 ` [ruby-core:103371] [Ruby master Feature#17753] Add Module#namespace fxn
2021-04-10 10:54 ` [ruby-core:103372] " fxn
2021-04-10 20:45 ` fxn [this message]
2021-04-12 15:28 ` [ruby-core:103405] " tenderlove
2021-04-12 17:29 ` [ruby-core:103409] " eregontp
2021-04-13  9:29 ` [ruby-core:103417] " fxn
2021-04-13  9:37 ` [ruby-core:103418] " fxn
2021-04-13  9:46 ` [ruby-core:103419] " fxn
2021-04-13 10:01 ` [ruby-core:103420] " fxn
2021-04-13 10:57 ` [ruby-core:103422] " eregontp
2021-04-13 11:50 ` [ruby-core:103424] " fxn
2021-04-13 16:35 ` [ruby-core:103433] " fxn
2021-04-17  7:27 ` [ruby-core:103493] " mame
2021-04-17  8:04 ` [ruby-core:103496] " fxn
2022-07-01  6:52 ` [ruby-core:109108] " ioquatix (Samuel Williams)
2022-07-01 16:13 ` [ruby-core:109111] " sawa (Tsuyoshi Sawada)
2022-07-02  0:03 ` [ruby-core:109112] " ioquatix (Samuel Williams)
2022-07-02  5:01 ` [ruby-core:109114] " sawa (Tsuyoshi Sawada)
2022-07-03 23:41 ` [ruby-core:109129] " ioquatix (Samuel Williams)
2022-07-04  1:42 ` [ruby-core:109131] " austin (Austin Ziegler)
2023-02-08  3:41 ` [ruby-core:112273] " shioyama (Chris Salzberg) via ruby-core
2023-02-08  7:54 ` [ruby-core:112277] " fxn (Xavier Noria) via ruby-core
2023-02-08 10:01 ` [ruby-core:112285] " fxn (Xavier Noria) via ruby-core
2023-02-09  8:02 ` [ruby-core:112295] " shioyama (Chris Salzberg) via ruby-core
2023-02-10  0:48 ` [ruby-core:112316] " shioyama (Chris Salzberg) via ruby-core

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-91475.20210410204534.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).