ruby-changes:66649
From: Masataka <ko1@a...>
Date: Wed, 30 Jun 2021 20:55:34 +0900 (JST)
Subject: [ruby-changes:66649] e8c2b03ee1 (master): [ruby/irb] Fix error on `ls object_cant_define_singleton`
https://git.ruby-lang.org/ruby.git/commit/?id=e8c2b03ee1 From e8c2b03ee145e10217a8ed58dbadfe30f9edbdfe Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara <kuwabara@p...> Date: Tue, 29 Jun 2021 19:16:58 +0900 Subject: [ruby/irb] Fix error on `ls object_cant_define_singleton` such as `ls 42`, `ls :sym` and so on https://github.com/ruby/irb/commit/b1d436a853 --- lib/irb/cmd/ls.rb | 5 +++-- test/irb/test_cmd.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index cfe513e..9cbba50 100644 --- a/lib/irb/cmd/ls.rb +++ b/lib/irb/cmd/ls.rb @@ -24,9 +24,10 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/ls.rb#L24 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 diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 2c7121d..3db490c 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -421,6 +421,25 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L421 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", -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/