[ruby/irb] Filter out top-level methods when using `ls

<Class/Module>`
(https://github.com/ruby/irb/pull/562)

Instead of always printing methods inherited from Class or Module, IRB by
default should filter them out unless `<Class/Module>` is specified to be
either of those.
This commit is contained in:
Stan Lo 2023-04-24 15:05:11 +01:00 committed by git
parent 886986b3ef
commit 805899dda2
2 changed files with 9 additions and 3 deletions

View File

@ -39,8 +39,12 @@ module IRB
def dump_methods(o, klass, obj)
singleton_class = begin obj.singleton_class; rescue TypeError; nil end
dumped_mods = Array.new
ancestors = klass.ancestors
ancestors = ancestors.reject { |c| c >= Object } if klass < Object
singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class }
# singleton_class' ancestors should be at the front
maps = class_method_map(singleton_class&.ancestors || [], dumped_mods) + class_method_map(klass.ancestors, dumped_mods)
maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods)
maps.each do |mod, methods|
name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
o.dump(name, methods)
@ -49,7 +53,6 @@ module IRB
def class_method_map(classes, dumped_mods)
dumped_methods = Array.new
classes = classes.reject { |mod| mod >= Object }
classes.map do |mod|
next if dumped_mods.include? mod

View File

@ -697,6 +697,8 @@ module TestIRB
assert_empty err
assert_match(/C2.methods:\s+m3\s+m5\n/, out)
assert_match(/C2#methods:\s+m3\s+m4\n.*M1#methods:\s+m2\n.*C1#methods:\s+m1\n/, out)
assert_not_match(/Module#methods/, out)
assert_not_match(/Class#methods/, out)
end
def test_ls_module
@ -716,8 +718,9 @@ module TestIRB
)
assert_empty err
assert_match(/M2\.methods:\s+m4\nModule#methods:/, out)
assert_match(/M2\.methods:\s+m4\n/, out)
assert_match(/M2#methods:\s+m1\s+m3\n.*M1#methods:\s+m2\n/, out)
assert_not_match(/Module#methods/, out)
end
def test_ls_instance