From b1e82a09dc144b2c29f4416904a496d8aca264fe Mon Sep 17 00:00:00 2001 From: Julia Boutin Date: Fri, 27 Mar 2026 14:33:54 -0600 Subject: [PATCH] Call methods on owned classes in ls command Previously, when calling comparison methods on receivers we don't control (`c >= Object`), there was a chance it could lack or have overridden the method. This commit flips them to prevent this `Object <= c` --- lib/irb/command/ls.rb | 4 ++-- test/irb/test_command.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/irb/command/ls.rb b/lib/irb/command/ls.rb index eae643ff4..0ffea144f 100644 --- a/lib/irb/command/ls.rb +++ b/lib/irb/command/ls.rb @@ -72,8 +72,8 @@ def dump_methods(o, klass, obj) singleton_class = begin Kernel.instance_method(:singleton_class).bind(obj).call; 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 } + ancestors = ancestors.reject { |c| Object <= c } if Object > klass + singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| Class <= c } # singleton_class' ancestors should be at the front maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods) diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index fd98a5a1b..e032f9d69 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -757,6 +757,16 @@ def test_ls_with_no_singleton_class assert_match(/Numeric#methods:\s+/, out) assert_match(/Integer#methods:\s+/, out) end + + def test_ls_with_object_altering_comparison_methods + out, err = execute_lines( + "class BadCompare; class << self; undef >=; undef <; end; def foo; end; end\n", + "obj = BadCompare.new\n", + "ls obj\n", + ) + assert_empty err + assert_match(/BadCompare#methods:\s+foo/, out) + end end class ShowDocTest < CommandTestCase