1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/irb] Fix error on ls object_cant_define_singleton

such as `ls 42`, `ls :sym` and so on

b1d436a853
This commit is contained in:
Masataka Pocke Kuwabara 2021-06-29 19:16:58 +09:00 committed by git
parent 0feec79846
commit e8c2b03ee1
2 changed files with 22 additions and 2 deletions

View file

@ -24,9 +24,10 @@ module IRB
end
def dump_methods(o, klass, obj)
maps = class_method_map(obj.singleton_class.ancestors)
singleton_class = obj.singleton_class rescue nil
maps = class_method_map((singleton_class || klass).ancestors)
maps.each do |mod, methods|
name = mod == obj.singleton_class ? "#{klass}.methods" : "#{mod}#methods"
name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
o.dump(name, methods)
end
end

View file

@ -421,6 +421,25 @@ module TestIRB
assert_match(/C.methods:\s+m5\n/m, out)
end
def test_ls_with_no_singleton_class
input = TestInputMethod.new([
"ls 42",
])
IRB.init_config(nil)
workspace = IRB::WorkSpace.new(self)
IRB.conf[:VERBOSE] = false
irb = IRB::Irb.new(workspace, input)
IRB.conf[:MAIN_CONTEXT] = irb.context
irb.context.return_format = "=> %s\n"
out, err = capture_output do
irb.eval_input
end
assert_empty err
assert_match(/Comparable#methods:\s+/, out)
assert_match(/Numeric#methods:\s+/, out)
assert_match(/Integer#methods:\s+/, out)
end
def test_show_source
input = TestInputMethod.new([
"show_source 'IRB.conf'\n",