Pry Not Stopping When Called from a Ruby Script That Reads from Stdin

Pry not stopping when called from a Ruby script that reads from stdin

Try ARGF with simple:

require 'rubygems'
require 'pry'
binding.pry

Now IO operations are not covered internally by ARGF.read and it became evident what’s wrong here. ARGF is “glued” to STDIN, therefore whatever is being passed to STDIN goes directly to pry’s input.

I do not know exactly, what instruction in your file.txt forces pry to quit, but there is one.


UPDATE

It looks like if a Ruby script yields anything on stdin (e. g. through a pipe,) both $stdin and STDIN are set to this pipe, messing up pry’s “where am I run from” detection.

I came up with this not-so-elegant solution:

# read input from ARGF
text = ARGF.read

# prepare new stdin to satisfy pry
pry_fd_stdin = IO.sysopen("/dev/tty")
pry_stdin = IO.new(pry_fd_stdin, "r")

# load pry and cheat it with our stdio
require 'pry'
Pry.config.input = pry_stdin

binding.pry

This solution has some glitches (e.g. pry prompt is shown only after input).

Is there a way to stop a long running command without exiting out of the whole pry session?

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.

__FILE__ returns different value when using binding.pry

Use _file_ instead of __FILE__. For example, given two files:

# foo.rb
require 'pry'
require './bar'
binding.pry
b = Bar.new

and:

# bar.rb
require 'pry'
class Bar
def initialize
binding.pry
end
end

Run them with ruby foo.rb:

ruby foo.rb

From: /Users/username/foo.rb @ line 3 :

1: require 'pry'
2: require './bar'
=> 3: binding.pry
4: b = Bar.new

(main):1 ⇒ _file_
=> "/Users/username/foo.rb"
(main):2 ⇒ exit

From: /Users/username/bar.rb @ line 4 Bar#initialize:

3: def initialize
=> 4: binding.pry
5: end

(#<Bar:0x00007fbb6caaff08>):1 ⇒ _file_
=> "/Users/username/bar.rb"

_file_ and any other local variable names can be found in binding.local_variables.

Executing a .rb file (Ruby file) with PRY in Windows 7

  1. Make sure you've installed pry and pry-debugger gems
  2. At the top of your file, add statements for require 'pry' and require 'pry-debugger'
  3. In your code, wherever you want to start ddebugging, just add a statement binding.pry
  4. Now, you can run your file like ruby filename.rb and the debugger should open

Why is gets throwing an error when arguments are passed to my ruby script?

Ruby will automatically treat unparsed arguments as filenames, then open and read the files making the input available to ARGF ($<). By default, gets reads from ARGF. To bypass that:

$stdin.gets

It has been suggested that you could use STDIN instead of $stdin, but it's usually better to use $stdin.

Additionally, after you capture the input you want from ARGV, you can use:

ARGV.clear

Then you'll be free to gets without it reading from files you may not have intended to read.

Ruby, pry: Can I add something to the command `pry example.rb` so pry automatically goes interactive when it finishes executing the script?

Not yet, but file an issue and i'll add it :)



Related Topics



Leave a reply



Submit