Skip to content

Commit d6cca2f

Browse files
committed
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`
1 parent b140bfc commit d6cca2f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/irb/command/ls.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def dump_methods(o, klass, obj)
7272
singleton_class = begin Kernel.instance_method(:singleton_class).bind(obj).call; rescue TypeError; nil end
7373
dumped_mods = Array.new
7474
ancestors = klass.ancestors
75-
ancestors = ancestors.reject { |c| c >= Object } if klass < Object
76-
singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class }
75+
ancestors = ancestors.reject { |c| Object <= c } if klass < Object
76+
singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| Class <= c }
7777

7878
# singleton_class' ancestors should be at the front
7979
maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods)

test/irb/test_command.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,16 @@ def test_ls_with_no_singleton_class
757757
assert_match(/Numeric#methods:\s+/, out)
758758
assert_match(/Integer#methods:\s+/, out)
759759
end
760+
761+
def test_ls_when_class_overrides_comparison
762+
out, err = execute_lines(
763+
"class BadCompare; class << self; undef >=; end; def foo; end; end\n",
764+
"obj = BadCompare.new\n",
765+
"ls obj\n",
766+
)
767+
assert_empty err
768+
assert_match(/BadCompare#methods:\s+foo/, out)
769+
end
760770
end
761771

762772
class ShowDocTest < CommandTestCase

0 commit comments

Comments
 (0)