1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Show instance methods of modules by default in ls

This commit is contained in:
Conrad Irwin 2012-04-16 23:28:24 -07:00
parent b5d044eeb2
commit 5e32fd39c6
2 changed files with 24 additions and 6 deletions

View file

@ -50,6 +50,7 @@ class Pry
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 @@ class Pry
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 @@ class Pry
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 @@ class Pry
$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

View file

@ -62,6 +62,10 @@ describe "ls" do
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