From: "Eregon (Benoit Daloze)" Date: 2022-06-09T10:23:59+00:00 Subject: [ruby-core:108833] [Ruby master Bug#18435] Calling `protected` on ancestor method changes result of `instance_methods(false)` Issue #18435 has been updated by Eregon (Benoit Daloze). I believe the only way that makes sense here is to remove "ZSUPER methods" altogether. Other Ruby implementations do not have this needless complexity and near-impossible-to-understand semantics. https://bugs.ruby-lang.org/issues/18751#note-11 might help to be closer, but IMHO we should remove "ZSUPER methods" altogether. `public/private/protected` should shallow-copy a method entry (but still change `Method#owner` of course), just like `alias_method` already behaves. This is what TruffleRuby and JRuby do, it's simpler, it is what Ruby users expect (Method is a Ruby object that captures a method entry, at the time it was requested), and it is consistent (`.owner` is always the module which has that method entry in its method table: #18729). For instance this should be `:p1\n:orig1` but currently it's `:p2\n:orig1` on CRuby. I claim no Ruby user expects that, because `Method` should capture a specific method entry, that's why we have bind/call and that's how Method objects are used. ```ruby class P private def m :p1 end public def orig :orig1 end end class C < P public :m alias_method :alias, :orig end class P private def m :p2 end public def orig :orig2 end end p C.new.m p C.new.alias ``` @matz OK to remove "ZSUPER methods" and make public/protected/private much simpler by having them shallow copy method entries, just like alias_method already does it? This will solve a lot of confusion and inconsistency for Method objects of methods defined by public/protected/private. ---------------------------------------- Bug #18435: Calling `protected` on ancestor method changes result of `instance_methods(false)` https://bugs.ruby-lang.org/issues/18435#change-97909 * Author: ufuk (Ufuk Kayserilioglu) * Status: Open * Priority: Normal * ruby -v: ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin20] * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- As documented `instance_methods(false)` works as follows: ```ruby module A def method1() end end class B include A def method2() end end p B.instance_methods(false) #=> [:method2] ``` However, calling `protected` on the method defined by `A`, unexpectedly changes the result of `instance_methods(false)` on `B`, even though the owner of the method is still `A`: ```ruby module A def method1() end end class B include A protected :method1 def method2() end end p B.instance_methods(false) #=> [:method1, :method2] p B.instance_method(:method1).owner #=> A ``` In contrast, calling `private` or `public` on the same method does not cause any changes on the result of `B.instance_methods(false)`. This feels like a bug in the implementation of `instance_methods(false)`, but, if it is by design, it should at least be documented on `Module#instance_methods`. This reproduction script gives the same output all the way from Ruby 2.0 up to Ruby-HEAD: https://wandbox.org/permlink/LqbXMBTYxURRZmDz -- https://bugs.ruby-lang.org/ Unsubscribe: