Project

General

Profile

Actions

Feature #14332

closed

Module.used_refinements to list refinement modules

Added by Eregon (Benoit Daloze) about 6 years ago. Updated about 2 years ago.

Status:
Closed
Target version:
-
[ruby-core:84694]

Description

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:

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


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #12737: Module#defined_refinementsClosedshugo (Shugo Maeda)Actions

Updated by shugo (Shugo Maeda) about 6 years ago

  • Status changed from Open to Assigned

Eregon (Benoit Daloze) wrote:

Module.used_modules was added in #7418.
But I think Module.used_refinements is more useful or at least complementary.

I'm for it, but I'd like to hear others' opinions.

Actions #2

Updated by naruse (Yui NARUSE) over 5 years ago

  • Target version deleted (2.6)

Updated by shugo (Shugo Maeda) over 2 years ago

  • Assignee changed from shugo (Shugo Maeda) to matz (Yukihiro Matsumoto)

Matz, can I add Module.used_refinements?

Actions #4

Updated by Eregon (Benoit Daloze) over 2 years ago

Actions #5

Updated by Eregon (Benoit Daloze) over 2 years ago

  • Description updated (diff)

Updated by shugo (Shugo Maeda) over 2 years ago

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

Updated by Eregon (Benoit Daloze) over 2 years ago

For Module.used_refinements, I believe it has to be an Array, not a Hash.
We can see in the description:

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

The refined classes are not unique, so we can't key by refined class.
And the order matters, so a Hash[namespace => [refined classes]] would be bad and needlessly complicated.

Since the spec of Module.used_refinements seems clear, I suggest to add it soon.

Updated by shugo (Shugo Maeda) over 2 years ago

Eregon (Benoit Daloze) wrote in #note-7:

For Module.used_refinements, I believe it has to be an Array, not a Hash.
We can see in the description:

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

The refined classes are not unique, so we can't key by refined class.
And the order matters, so a Hash[namespace => [refined classes]] would be bad and needlessly complicated.

Agreed.

Since the spec of Module.used_refinements seems clear, I suggest to add it soon.

Matz, what do you think of it?

https://github.com/shugo/ruby/compare/used_refinements

Updated by matz (Yukihiro Matsumoto) over 2 years ago

It's too late for 3.1. But after the release, I basically honor @shugo's decision here.

Matz.

Updated by shugo (Shugo Maeda) over 2 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to shugo (Shugo Maeda)

Updated by shugo (Shugo Maeda) about 2 years ago

  • Status changed from Assigned to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0