Skip to content

Commit 384f338

Browse files
authored
Make ls command work for BasicObjects (#1177)
* Add ls command test * Make ls command work for BasicObjects * Use is_a? to determine if obj is a Class or Module * Use === operator to determin if obj is a Class or Module
1 parent f5cb9d5 commit 384f338

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

lib/irb/command/ls.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,21 @@ def execute(arg)
5555

5656
o = Output.new(grep: grep)
5757

58-
klass = (obj.class == Class || obj.class == Module ? obj : obj.class)
58+
klass = Kernel.instance_method(:class).bind(obj).call
59+
obj_is_class_or_module = Module === obj
60+
klass = obj_is_class_or_module ? obj : klass
5961

60-
o.dump("constants", obj.constants) if obj.respond_to?(:constants)
62+
o.dump("constants", obj.constants) if obj_is_class_or_module
6163
dump_methods(o, klass, obj)
62-
o.dump("instance variables", obj.instance_variables)
64+
o.dump("instance variables", Kernel.instance_method(:instance_variables).bind(obj).call)
6365
o.dump("class variables", klass.class_variables)
6466
o.dump("locals", locals) if locals
6567
o.print_result
6668
rescue EvaluationError
6769
end
6870

6971
def dump_methods(o, klass, obj)
70-
singleton_class = begin obj.singleton_class; rescue TypeError; nil end
72+
singleton_class = begin Kernel.instance_method(:singleton_class).bind(obj).call; rescue TypeError; nil end
7173
dumped_mods = Array.new
7274
ancestors = klass.ancestors
7375
ancestors = ancestors.reject { |c| c >= Object } if klass < Object

test/irb/command/test_ls.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require "tempfile"
2+
require_relative "../helper"
3+
4+
module TestIRB
5+
class LSTest < IntegrationTestCase
6+
def setup
7+
super
8+
9+
write_ruby <<~'RUBY'
10+
class Foo
11+
class Bar
12+
def bar
13+
"this is bar"
14+
end
15+
end
16+
17+
def foo
18+
"this is foo"
19+
end
20+
end
21+
22+
class BO < BasicObject
23+
ONE = 1
24+
def baz
25+
"this is baz"
26+
end
27+
end
28+
29+
binding.irb
30+
RUBY
31+
end
32+
33+
def test_ls_class
34+
out = run_ruby_file do
35+
type "ls Foo"
36+
type "exit"
37+
end
38+
39+
assert_match(/constants: Bar/, out)
40+
assert_match(/Foo#methods: foo/, out)
41+
end
42+
43+
def test_ls_instance
44+
out = run_ruby_file do
45+
type "ls Foo.new"
46+
type "exit"
47+
end
48+
49+
assert_match(/Foo#methods: foo/, out)
50+
end
51+
52+
def test_ls_basic_object
53+
out = run_ruby_file do
54+
type "ls BO"
55+
type "exit"
56+
end
57+
58+
assert_match(/constants:.*ONE/, out)
59+
assert_match(/BO#methods: baz/, out)
60+
end
61+
62+
def test_ls_basic_object_instance
63+
out = run_ruby_file do
64+
type "ls BO.new"
65+
type "exit"
66+
end
67+
68+
assert_match(/BO#methods: baz/, out)
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)