ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "sawa (Tsuyoshi Sawada)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109111] [Ruby master Feature#17753] Add Module#namespace
Date: Fri, 01 Jul 2022 16:13:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-98258.20220701161309.73@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17753.20210326191942.73@ruby-lang.org

Issue #17753 has been updated by sawa (Tsuyoshi Sawada).


ioquatix (Samuel Williams) wrote in #note-21:
> ```ruby
> class A::B::C::MyClass; end
> 
> A::B::C::MyClass.name(0) # -> "MyClass"
> A::B::C::MyClass.name(1) # -> "C::MyClass"
> A::B::C::MyClass.name(-1) # -> "A::B::C"
> A::B::C::MyClass.name(-2) # -> "A::B"
> ```

What is the rule behind what the argument represents? To me, your four examples except for the first one seem to suggest:

1. The nesting levels (achieved by separating the full name  by `::`) can be referred to by an index as if they were placed in an array. 
2. a. If the argument is negative, then remove the nesting levels from the one indexed by the argument up to the last one.
 b. If the argument is non-negative, then remove the nesting levels from the first one up to the one indexed by the argument.
3. Join the remaining nesting levels with `::`.

But, then I would expect:

```ruby
A::B::C::MyClass.name(0) # -> "B::C::MyClass"
```

contrary to what you wrote.

What is your intended logic? Is it coherent? And does that provide a way to get `"B::C::MyClass"`?

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

* 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:[~2022-07-01 16:13 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 ` [ruby-core:103382] " fxn
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 ` sawa (Tsuyoshi Sawada) [this message]
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-98258.20220701161309.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).