diff options
| author | Masataka Pocke Kuwabara <kuwabara@pocke.me> | 2021-06-29 19:16:58 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2021-06-30 20:55:18 +0900 |
| commit | e8c2b03ee145e10217a8ed58dbadfe30f9edbdfe (patch) | |
| tree | 9d23efae4b85d88b9be6f7929b5f67db90b6685f | |
| parent | 0feec7984622f0ceb0514b3b340203fdc51550dc (diff) | |
[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
| -rw-r--r-- | lib/irb/cmd/ls.rb | 5 | ||||
| -rw-r--r-- | 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 cfe513eb5f..9cbba50f10 100644 --- a/lib/irb/cmd/ls.rb +++ b/lib/irb/cmd/ls.rb @@ -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 diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 2c7121df5b..3db490ceab 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -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", |
