From 73a568eac1df63045bcae055715de230857b7901 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 25 Feb 2011 20:31:38 +1300 Subject: [PATCH] exit and exit-all support return values --- lib/pry/commands.rb | 12 +++++++----- lib/pry/pry_instance.rb | 11 +++++++++-- lib/pry/version.rb | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/pry/commands.rb b/lib/pry/commands.rb index 805d2477a..61ae51619 100644 --- a/lib/pry/commands.rb +++ b/lib/pry/commands.rb @@ -75,11 +75,12 @@ class Commands < CommandBase output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}." end - command "exit-all", "End all nested Pry sessions. Aliases: !!" do - throw(:breakout, 0) + command "exit-all", "End all nested Pry sessions. Accepts optional return value. Aliases: @!" do + str = opts[:val].split.drop(1).join(' ') + throw(:breakout, [0, target.eval(str)]) end - alias_command "!!", "exit-all", "" + alias_command "@!", "exit-all", "" command "ls", "Show the list of vars in the current scope. Type `ls --help` for more info." do |*args| options = {} @@ -465,8 +466,9 @@ class Commands < CommandBase end end - command "exit", "End the current Pry session. Aliases: quit, back" do - throw(:breakout, opts[:nesting].level) + command "exit", "End the current Pry session. Accepts optional return value. Aliases: quit, back" do + str = opts[:val].split.drop(1).join(' ') + throw(:breakout, [opts[:nesting].level, target.eval(str)]) end alias_command "quit", "exit", "" diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index e019935b6..105714171 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -86,7 +86,7 @@ def repl(target=TOPLEVEL_BINDING) target.eval("_pry_ = Pry.active_instance") target.eval("_ = Pry.last_result") - break_level = catch(:breakout) do + break_data = catch(:breakout) do nesting.push [nesting.size, target_self, self] loop do rep(target) @@ -97,11 +97,18 @@ def repl(target=TOPLEVEL_BINDING) exec_hook :after_session, output, target_self + # If break_data is an array, then the last element is the return value + break_level, return_value = Array(break_data) + # keep throwing until we reach the desired nesting level if nesting_level != break_level - throw :breakout, break_level + throw :breakout, break_data end + # if one was provided, return the return value + return return_value if return_value + + # otherwise return the target_self target_self end diff --git a/lib/pry/version.rb b/lib/pry/version.rb index 76667bba8..fc9500420 100644 --- a/lib/pry/version.rb +++ b/lib/pry/version.rb @@ -1,3 +1,3 @@ class Pry - VERSION = "0.6.1pro1" + VERSION = "0.6.1pro3" end