Permalink
Browse files

Show instance methods of modules by default in ls

  • Loading branch information...
1 parent b5d044e commit 5e32fd39c6705da94b182528c22163c5d0814587 @ConradIrwin ConradIrwin committed Apr 17, 2012
Showing with 24 additions and 6 deletions.
  1. +20 −6 lib/pry/default_commands/ls.rb
  2. +4 −0 test/test_default_commands/test_ls.rb
@@ -50,6 +50,7 @@ def process
opts.present?(:ivars))
show_methods = opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) || !has_opts
+ show_self_methods = (!has_opts && Module === obj)
show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
show_ivars = opts.present?(:ivars) || !has_opts
show_locals = opts.present?(:locals) || (!has_opts && args.empty?)
@@ -76,7 +77,7 @@ def process
if show_methods
# methods is a hash {Module/Class => [Pry::Methods]}
- methods = all_methods(obj).select{ |method| opts.present?(:ppp) || method.visibility == :public }.group_by(&:owner)
+ methods = all_methods(obj).group_by(&:owner)
# reverse the resolution order so that the most useful information appears right by the prompt
resolution_order(obj).take_while(&below_ceiling(obj)).reverse.each do |klass|
@@ -85,6 +86,14 @@ def process
end
end
+ if show_self_methods
+ methods = all_methods(obj, true).select{ |m| m.owner == obj && m.name =~ grep_regex }
+ if methods.first
+ methods_here = format_methods(methods.select{ |m| m.name =~ grep_regex })
+ output_section "#{Pry::WrappedModule.new(methods.first.owner).method_prefix}methods", methods_here
+ end
+ end
+
if show_ivars
klass = (Module === obj ? obj : obj.class)
ivars = Pry::Method.safe_send(obj, :instance_variables)
@@ -115,13 +124,18 @@ def process
$LAST_PAREN_MATCH $LAST_READ_LINE $MATCH $POSTMATCH $PREMATCH)
# Get all the methods that we'll want to output
- def all_methods(obj)
- methods = opts.present?(:'instance-methods') ? Pry::Method.all_from_class(obj) : Pry::Method.all_from_obj(obj)
+ def all_methods(obj, instance_methods=false)
+ methods = if instance_methods || opts.present?(:'instance-methods')
+ Pry::Method.all_from_class(obj)
+ else
+ Pry::Method.all_from_obj(obj)
+ end
+
if jruby? && !opts.present?(:J)
- trim_jruby_aliases(methods)
- else
- methods
+ methods = trim_jruby_aliases(methods)
end
+
+ methods.select{ |method| opts.present?(:ppp) || method.visibility == :public }
end
# JRuby creates lots of aliases for methods imported from java in an attempt to
@@ -62,6 +62,10 @@
it "should work for ivars" do
mock_pry("module StigmaT1sm; def foobie; @@gharble = 456; end; end", "Object.new.tap{ |o| o.extend(StigmaT1sm) }.foobie", "cd StigmaT1sm", "ls -i").should =~ /@@gharble/
end
+
+ it "should include instance methods by default" do
+ mock_pry("ls Module.new{ def shinanagarns; 4; end }").should =~ /shinanagarns/
+ end
end
describe "constants" do

0 comments on commit 5e32fd3

Please sign in to comment.