[ruby-core:114675] [Ruby master Feature#19871] Add __owner__

Issue #19871 has been reported by konsolebox (K B). ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/

Issue #19871 has been updated by konsolebox (K B). 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: ``` ruby 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 ``` ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871#change-104506 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/

Issue #19871 has been updated by mame (Yusuke Endoh). Could you explain the use case? ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871#change-104514 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/

Issue #19871 has been updated by nobu (Nobuyoshi Nakada). Why not use the constant without scopes? ```ruby module M C = 1234 def m puts "method(__method__).owner: #{method(__method__).owner}" puts "method(__method__).owner::C: #{method(__method__).owner::C}" puts "C: #{C}" #=> 1234 end end ``` ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871#change-104515 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/

Issue #19871 has been updated by konsolebox (K B). nobu (Nobuyoshi Nakada) wrote in #note-3:
Why not use the constant without scopes?
```ruby 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. ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871#change-104529 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/

Issue #19871 has been updated by konsolebox (K B). 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. ---------------------------------------- Feature #19871: Add __owner__ https://bugs.ruby-lang.org/issues/19871#change-104530 * Author: konsolebox (K B) * Status: Open * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/
participants (3)
-
konsolebox (K B)
-
mame (Yusuke Endoh)
-
nobu (Nobuyoshi Nakada)