Skip to content

Commit

Permalink
Show instance methods of modules by default in ls
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Apr 17, 2012
1 parent b5d044e commit 5e32fd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/pry/default_commands/ls.rb
Expand Up @@ -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?)
Expand All @@ -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|
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/test_default_commands/test_ls.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5e32fd3

Please sign in to comment.