From 805899dda29e36a42ffd9e076b9296f3bf13af5a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Apr 2023 15:05:11 +0100 Subject: [ruby/irb] Filter out top-level methods when using `ls ` (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 `` is specified to be either of those. --- lib/irb/cmd/ls.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index d5a371a4d6..063a88d33f 100644 --- a/lib/irb/cmd/ls.rb +++ b/lib/irb/cmd/ls.rb @@ -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 -- cgit v1.2.1