Feature #19871
closed
Added by konsolebox (K B) 11 months ago.
Updated 11 months ago.
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.
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
Could you explain the use case?
Why not use the constant without scopes?
module M
C = 1234
def m
puts "C: #{C}" #=> C: 1234
end
end
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.
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.
- Status changed from Open to Rejected
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0