0

I want something to stop a Pry command but I don't want to exit to my shell.

Control+C is not what I'm looking for. I'm not trying to exit out of a loop or out of the entire session. I simply want to return to the Pry prompt if I run a line of code that takes a long time.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
David West
  • 2,256
  • 6
  • 32
  • 62
  • possible duplicate of [How do I step out of a loop with Ruby Pry?](http://stackoverflow.com/questions/8015531/how-do-i-step-out-of-a-loop-with-ruby-pry) – Rossiar Sep 11 '15 at 15:27
  • To start I just want to abort the line of code I typed and return to the pry prompt – David West Sep 11 '15 at 15:49

1 Answers1

1

This is possible if you started Pry from the command line (that is, by just running pry), but probably not if you're inside a binding.pry during another process.

If you started Pry directly, it set up signal handlers and will catch Control+C to return you to the REPL. IRB does the same thing, depending on its ignore_sigint setting.

But, if you're running inside another process, that process may have set up its own signal handlers. E.g., both rspec and rails s watch for Control+C to perform a graceful shutdown. Trying to stop a command inside one of those processes will actually leave Pry open, but perform the process's shutdown work, so as soon as you exit Pry, you're dumped back to the command line.

You can in your Pry session set up your own signal handler, e.g.:

Signal.trap('SIGINT') { puts 'Trapped' }

And there may be something exceedingly clever you can do there to move execution back to the Pry REPL, but it's almost certain to wreak havoc with other handlers that might be in place.

Community
  • 1
  • 1
Kristján
  • 18,165
  • 5
  • 50
  • 62