Project

General

Profile

Actions

Feature #19871

closed

Add __owner__

Feature #19871: Add __owner__

Added by konsolebox (K B) about 2 years ago. Updated about 2 years 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) about 2 years ago Actions #1 [ruby-core:114677]

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) about 2 years ago Actions #2 [ruby-core:114686]

Could you explain the use case?

Updated by nobu (Nobuyoshi Nakada) about 2 years ago Actions #3 [ruby-core:114687]

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) about 2 years ago Actions #4 [ruby-core:114699]

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) about 2 years ago Actions #5 [ruby-core:114700]

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.

Updated by Eregon (Benoit Daloze) about 2 years ago Actions #6

  • Status changed from Open to Rejected
Actions

Also available in: PDF Atom