From 8f1d48236c31cf9d37587a4a699234012ff1eead Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 22 Apr 2011 18:09:27 +1200 Subject: [PATCH] juts doing a quick save --- README.markdown | 42 ++++++++++++++++++++++++++++++++++++ lib/pry/command_processor.rb | 5 +++-- lib/pry/commands.rb | 12 +++++++++-- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 8596549d..df52db15 100644 --- a/README.markdown +++ b/README.markdown @@ -171,6 +171,8 @@ current directory and count the number of lines in that file with ### Code Browsing +#### show-method + You can browse method source code with the `show-method` command. Nearly all Ruby methods (and some C methods, with the pry-doc gem) can have their source viewed. Code that is longer than a page is sent through a pager (such as less), and all code is properly syntax @@ -224,7 +226,47 @@ Note that we can also view C methods (from Ruby Core) using the return result; } +#### Special locals +Some commands such as `show-method`, `show-doc`, `show-command` `stat` +and `cat` update the `_file_` and `_dir_` local variables after they +run. These locals contain the full path to the file involved in the +last command as well as the directory containing that file. + +You can then use these special locals in conjunction with shell +commands to do such things as change directory into the directory +containing the file, open the file in an editor, `cat` out the entire +file, and so on. + +In the following example we wil use Pry to fix a bug in a method: + + pry(main)> greet "john" + hello johnhow are you?=> nil + pry(main)> show-method greet + + From: /Users/john/ruby/play/bug.rb @ line 2: + Number of lines: 4 + + def greet(name) + print "hello #{name}" + print "how are you?" + end + pry(main)> .emacsclient #{_file_} + pry(main)> load _file_ + pry(main)> greet "john" + hello john + how are you? + => nil + pry(main)> show-method greet + + From: /Users/john/ruby/play/bug.rb @ line 2: + Number of lines: 4 + + def greet(name) + puts "hello #{name}" + puts "how are you?" + end + Features and limitations ------------------------ diff --git a/lib/pry/command_processor.rb b/lib/pry/command_processor.rb index 6c99c800..0a0902e4 100644 --- a/lib/pry/command_processor.rb +++ b/lib/pry/command_processor.rb @@ -71,12 +71,13 @@ class Pry @@cd_history << Dir.pwd Dir.chdir(dest) - output.puts "=> #{dest}" rescue Errno::ENOENT output.puts "No such directory: #{dest}" end else - system(cmd) + if !system(cmd) + output.puts "Error: could not execute system command #{cmd}" + end end # Tick, tock, im getting rid of this shit soon. diff --git a/lib/pry/commands.rb b/lib/pry/commands.rb index a4bb2433..060a55de 100644 --- a/lib/pry/commands.rb +++ b/lib/pry/commands.rb @@ -110,8 +110,15 @@ e.g: stat hello_method next if opts.help? meth_name = args.shift - meth_name = meth_name_from_binding(target) if !meth_name - + if meth_name + if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/ && !opts.context? + context, meth_name = $1, $2 + target = Pry.binding_for(target.eval(context)) + end + else + meth_name = meth_name_from_binding(target) + end + if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil? output.puts "Invalid method name: #{meth_name}. Type `stat --help` for help" next @@ -123,6 +130,7 @@ e.g: stat hello_method output.puts make_header(meth, code_type, code) output.puts bold("Method Name: ") + meth_name + output.puts bold("Method Owner: ") + (meth.owner.to_s ? meth.owner.to_s : "Unknown") output.puts bold("Method Language: ") + code_type.to_s.capitalize output.puts bold("Method Type: ") + (meth.is_a?(Method) ? "Bound" : "Unbound") output.puts bold("Method Arity: ") + meth.arity.to_s