From: "ioquatix (Samuel Williams)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109112] [Ruby master Feature#17753] Add Module#namespace
Date: Sat, 02 Jul 2022 00:03:52 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-98259.20220702000351.73@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17753.20210326191942.73@ruby-lang.org
Issue #17753 has been updated by ioquatix (Samuel Williams).
```ruby
class Class
def name(offset = nil)
return super() unless offset
parts = super().split('::')
if offset >= 0
parts = parts[(parts.size - 1 - offset)..-1]
else
parts = parts[0...(parts.size + offset)]
end
return parts.join('::')
end
end
module A
module B
module C
class MyClass
end
end
end
end
pp A::B::C::MyClass.name(0) # -> "MyClass"
pp A::B::C::MyClass.name(1) # -> "C::MyClass"
pp A::B::C::MyClass.name(-1) # -> "A::B::C"
pp A::B::C::MyClass.name(-2) # -> "A::B"
```
Something like this.
----------------------------------------
Feature #17753: Add Module#namespace
https://bugs.ruby-lang.org/issues/17753#change-98259
* 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/
next prev parent reply other threads:[~2022-07-02 0:04 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 ` [ruby-core:109111] " sawa (Tsuyoshi Sawada)
2022-07-02 0:03 ` ioquatix (Samuel Williams) [this message]
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-98259.20220702000351.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).