Project

General

Profile

Actions

Feature #19871

closed

Add __owner__

Added by konsolebox (K B) 8 months ago. Updated 8 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:114675]

Description

Which will give the owner of the currently executing method.

method(__method__).owner or method(__callee__).owner isn't enough since it may return a different owner if the method is overridden.

Relying on calling the name of its owner is also theoretically not reliable since the constant can be overshadowed and it makes code less portable when moving it from one namespace to another.

Updated by konsolebox (K B) 8 months ago

The changes needed to implement this turned out to be just simple so I went ahead and just created a PR in https://github.com/ruby/ruby/pull/8411.

The difference between using method(__method__).owner and using __owner__ can be demonstrated in the following code:

module M
  C = 1234

  def m
    puts "method(__method__).owner: #{method(__method__).owner}"
    puts "method(__method__).owner::C: #{method(__method__).owner::C}"
    puts "__owner__: #{__owner__}"
    puts "__owner__::C: #{__owner__::C}"
  end
end

class N
  include M
  C = 5678

  def m
    super
  end
end

N.new.m

Output:

method(__method__).owner: N
method(__method__).owner::C: 5678
__owner__: M
__owner__::C: 1234

Updated by mame (Yusuke Endoh) 8 months ago

Could you explain the use case?

Updated by nobu (Nobuyoshi Nakada) 8 months ago

Why not use the constant without scopes?

module M
  C = 1234

  def m
    puts "C: #{C}" #=> C: 1234
  end
end

Updated by konsolebox (K B) 8 months ago

nobu (Nobuyoshi Nakada) wrote in #note-3:

Why not use the constant without scopes?

module M
  C = 1234

  def m
    puts "C: #{C}" #=> C: 1234
  end
end

I was expecting the lookup to still be affected by the inheriting class. I should have tested it first.

I guess this invalidates the feature request.

Updated by konsolebox (K B) 8 months ago

mame (Yusuke Endoh) wrote in #note-2:

Could you explain the use case?

It's supposed to make sure a general-use module can be implemented purely and can't be tainted by any of its inheritors, unless it's deliberately patched of course.

But as I have just discovered, without scopes a constant will remain as the constant that is accessed expectedly.

Actions #6

Updated by Eregon (Benoit Daloze) 8 months ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0