Skip to content

Commit

Permalink
exit and exit-all support return values
Browse files Browse the repository at this point in the history
  • Loading branch information
banister committed Feb 25, 2011
1 parent 7ce581e commit 73a568e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/pry/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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", ""
Expand Down
11 changes: 9 additions & 2 deletions lib/pry/pry_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/pry/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Pry
VERSION = "0.6.1pro1"
VERSION = "0.6.1pro3"
end

0 comments on commit 73a568e

Please sign in to comment.