From: "jeremyevans0 (Jeremy Evans)" Date: 2021-12-26T21:09:12+00:00 Subject: [ruby-core:106829] [Ruby master Bug#18435] Calling `protected` on ancestor method changes result of `instance_methods(false)` Issue #18435 has been updated by jeremyevans0 (Jeremy Evans). I don't think this is a bug, I think it is expected behavior. When you call `protected` in the class, it creates a method entry in the class with a different visibility, even if the owner of the method is still the module. Hopefully a committer with more experience can confirm that. I don't think this should be documented in `instance_methods`. After all, it is not unique to `instance_methods`, but all related methods (`methods`, `protected_instance_methods`, etc.). If we document this behavior at all, it would be better to document the creation of method entries in `doc/syntax/modules_and_classes.rdoc`, in the section on `Visibility`. However, I don't think that this behavior is worth documenting. ---------------------------------------- Bug #18435: Calling `protected` on ancestor method changes result of `instance_methods(false)` https://bugs.ruby-lang.org/issues/18435#change-95641 * 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: