Pry does not stop when called from a Ruby script that is read from stdin

I created a Ruby script console that uses ARGF to load data from a file or stdin, which then calls Pry.

This works fine when I transfer the file to (Pry pauses), but it fails (Pry does not stop and just exits Ruby) when I transfer my data using stdin.

It's weird, does anyone know why? I would like to pass data through stdin and pause Pry.

Here is an example script:

require 'rubygems' require 'pry' def pry_it(str) binding.pry end pry_it(ARGF.read) 

When I call this application with a file in ARGV, I get my correct answer - pry pauseing

 % bundle exec ruby pry_test.rb file.txt From: /Users/wilcoxr/Development/json_pry/pry_test.rb @ line 8 Object#pry_it: 6: def pry_it(str) 7: => 8: binding.pry 9: end [1] pry(main)> 

Fine! I can execute the Pry commands that I want

When I try to use STDIN to send data to my tool:

 % cat file.txt | bundle exec ruby pry_test.rb From: /Users/wilcoxr/Development/json_pry/pry_test.rb @ line 8 Object#pry_it: 6: def pry_it(str) 7: => 8: binding.pry 9: end [1] pry(main)> % 

Look carefully: notice that I returned to the shell prompt, but it does not stop at IRB. Weird! I do not understand why I get this behavior ....

+10
ruby pry
source share
1 answer

Try ARGF with a simple:

 require 'rubygems' require 'pry' binding.pry 

Now IO operations are not covered internally by ARGF.read , and it has become obvious that this is not the case. ARGF "glued" to STDIN , so everything that is passed to STDIN goes directly to pry .

I don’t know exactly which instruction in your file.txt makes pry exit, but it is.


UPDATE

It looks like the Ruby script will output something to stdin (for example, via a channel), and $stdin and STDIN tuned to this channel, which will ruin the pry "where am I running" from detection.

I came up with this not very 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 (for example, the pry request is displayed only after input).

+6
source share

All Articles