Skip to content

Commit

Permalink
Merge pull request #245 from neko314/sort_ls_result_ordered_by_anscestry
Browse files Browse the repository at this point in the history
Sort ls result ordered by anscestry
  • Loading branch information
aycabta authored Jun 22, 2021
2 parents b89e053 + fdd5c0a commit 46d1686
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
14 changes: 3 additions & 11 deletions lib/irb/cmd/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,20 @@ def execute(*arg, grep: nil)
klass = (obj.class == Class || obj.class == Module ? obj : obj.class)

o.dump("constants", obj.constants) if obj.respond_to?(:constants)
dump_singleton_methods(o, klass, obj)
dump_instance_methods(o, klass)
dump_methods(o, klass, obj)
o.dump("instance variables", obj.instance_variables)
o.dump("class variables", klass.class_variables)
o.dump("locals", locals)
end

def dump_singleton_methods(o, klass, obj)
maps = class_method_map(obj.singleton_class.ancestors.take_while { |c| c != klass })
def dump_methods(o, klass, obj)
maps = class_method_map(obj.singleton_class.ancestors)
maps.each do |mod, methods|
name = mod == obj.singleton_class ? "#{klass}.methods" : "#{mod}#methods"
o.dump(name, methods)
end
end

def dump_instance_methods(o, klass)
maps = class_method_map(klass.ancestors)
maps.each do |mod, methods|
o.dump("#{mod}#methods", methods)
end
end

def class_method_map(classes)
dumped = Set.new
classes.reject { |mod| mod >= Object }.map do |mod|
Expand Down
26 changes: 17 additions & 9 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,23 +377,30 @@ def test_irb_load

def test_ls
input = TestInputMethod.new([
"class C\n",
" def m1() end\n",
"class P\n",
" def m() end\n",
" def m2() end\n",
"end\n",

"module M\n",
"class C < P\n",
" def m1() end\n",
" def m2() end\n",
"end\n",

"module M\n",
" def m1() end\n",
" def m3() end\n",
"end\n",

"module M2\n",
" include M\n",
" def m3() end\n",
" def m4() end\n",
"end\n",

"obj = C.new\n",
"obj.instance_variable_set(:@a, 1)\n",
"obj.extend M2\n",
"def obj.m4() end\n",
"def obj.m5() end\n",
"ls obj\n",
])
IRB.init_config(nil)
Expand All @@ -407,10 +414,11 @@ def test_ls
end
assert_empty err
assert_match(/^instance variables:\s+@a\n/m, out)
assert_match(/C#methods:\s+m1\n/m, out)
assert_match(/M#methods:\s+m2\n/m, out)
assert_match(/M2#methods:\s+m3\n/m, out)
assert_match(/C.methods:\s+m4\n/m, out)
assert_match(/P#methods:\s+m\n/m, out)
assert_match(/C#methods:\s+m2\n/m, out)
assert_match(/M#methods:\s+m1\s+m3\n/m, out)
assert_match(/M2#methods:\s+m4\n/m, out)
assert_match(/C.methods:\s+m5\n/m, out)
end

def test_show_source
Expand Down

0 comments on commit 46d1686

Please sign in to comment.