From 06991b221a8736313a83cc8d79cca327c2939ecf Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 11 Sep 2011 04:41:51 +1200 Subject: [PATCH] added a Pry#run_command method Useful when pry instance is passed to procs such as exception handlers e.g Pry.config.exception_handler = proc |output, value, _pry_| _pry_.run_command('cat --ex') end Notw that run_command also accepts a optional second parameter 'binding' that defaults to binding_stack.last when not given --- lib/pry/pry_instance.rb | 10 ++++++++++ test/test_pry.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index f4f0ea90..ddd57684 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -319,6 +319,16 @@ class Pry end end + # Run the specified command. + # @param [String] The command (and its params) to execute. + # @param [Binding] The binding to use.. + # @example + # pry_instance.run_command("ls -m") + def run_command(val, target = binding_stack.last) + process_line(val, "", target) + Pry::CommandContext::VOID_VALUE + end + # Set the last result of an eval. # This method should not need to be invoked directly. # @param [Object] result The result. diff --git a/test/test_pry.rb b/test/test_pry.rb index b920a40b..e2889b7c 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -187,6 +187,19 @@ describe Pry do end end + describe "Pry#run_command" do + it 'should run a command in a specified context' do + b = Pry.binding_for(7) + p = Pry.new(:output => StringIO.new) + p.run_command("ls -m", b) + p.output.string.should =~ /divmod/ + end + + it 'should run a command in the context of a session' do + mock_pry("@session_ivar = 10", "_pry_.run_command('ls')").should =~ /@session_ivar/ + end + end + describe "repl" do describe "basic functionality" do it 'should set an ivar on an object and exit the repl' do