ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "shugo (Shugo Maeda)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:106166] [Ruby master Feature#14332] Module.used_refinements to list refinement modules
Date: Fri, 19 Nov 2021 02:14:41 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-94769.20211119021440.772@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14332.20180107233109.772@ruby-lang.org

Issue #14332 has been updated by shugo (Shugo Maeda).


Matz accepted Module.used_refinements at the developers meeting on 2021-11-18.

But we need to consider consistency of the return value with #12737, used_refinements will be introduced after #12737 is accepted (maybe in Ruby 3.2?).


----------------------------------------
Feature #14332: Module.used_refinements to list refinement modules
https://bugs.ruby-lang.org/issues/14332#change-94769

* Author: Eregon (Benoit Daloze)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
Module.used_modules was added in #7418.
But I think `Module.used_refinements` is more useful or at least complementary.

Refinements were implemented in TruffleRuby, and I found Module.used_refinements so useful that I left it there.

Instead of listing namespace modules (arguments to #using), it lists the refinement modules:

~~~ ruby
module Json
  refine Integer do
    def to_json
      to_s
    end
  end

  refine String do
    def to_json
      inspect
    end
  end
end

module Fact
  refine Integer do
    def fact
      self <= 1 ? 1 : self * (self-1).fact
    end
  end
end

using Json
p Module.used_modules # => [Json]
p Module.used_refinements # => [#<refinement:Integer@Json>, #<refinement:String@Json>]

using Fact
p Module.used_modules # => [Json, Fact]
p Module.used_refinements # => [#<refinement:Integer@Fact>, #<refinement:Integer@Json>, #<refinement:String@Json>]
~~~

This shows which classes are refined and by which refinement namespace.
It also shows if a class has multiple active refinements in the scope.
And, last but not least, the name of the method contains "refinements".
I find `used_modules` hard to remember and it doesn't sound related to refinements from the name (while looking at the Module's methods).



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

  parent reply	other threads:[~2021-11-19  2:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14332.20180107233109.772@ruby-lang.org>
2021-10-22  5:18 ` [ruby-core:105746] [Ruby master Feature#14332] Module.used_refinements to list refinement modules shugo (Shugo Maeda)
2021-11-19  2:14 ` shugo (Shugo Maeda) [this message]
2021-11-19 20:23 ` [ruby-core:106181] " Eregon (Benoit Daloze)
2021-11-19 23:38 ` [ruby-core:106185] " shugo (Shugo Maeda)
2021-12-09  7:15 ` [ruby-core:106572] " matz (Yukihiro Matsumoto)
2021-12-10  1:48 ` [ruby-core:106611] " shugo (Shugo Maeda)
2022-01-05  8:07 ` [ruby-core:106970] " shugo (Shugo Maeda)

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-94769.20211119021440.772@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).