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

后端 未结 1 731
谎友^
谎友^ 2020-12-30 08:24

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

This works great when I pass a file in (Pry pauses) but fa

相关标签:
1条回答
  • 2020-12-30 09:26

    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).

    0 讨论(0)
提交回复
热议问题