Search code examples
rubystdinshoes

Getting both File input AND STDIN from ARGF?


I am using the shoes library to run a piece of ruby code and have discovered that it treats the ruby code it's running as File Input, and thus does not allow me to get STDIN anymore (since ARGF allows File Input OR STDIN but apparently not both).

Is there anyway to override this? I'm told perl, for example, allows you to read from STDIN once the IO buffer is empty.

Edit:

I have had some success with the "-" special filename character, which apparently is a signal to switch to STDIN on the command line.

Previous Form of Question: Is Shoes ARGF Broken?

Using general Ruby, I can read either files or Standard In with ARGF. With Shoes, I am only able to read files. Anything from standard in just gets ignored. Is it eating standard in, or is there another way to access it?

Example code lines: Either stand alone in a ruby file, or inside a Shoes app in shoes.

#ruby testargf.rb aus.txt is the same as ruby testargf.rb<aus.txt
#but isn't in shoes. shoes only prints with the first input, not the second
ARGF.each do |line|  #readLine.each has same result
  puts line
end

Or in Shoes:

#shoes testargfshoes.rb aus.txt should be the same as <aus.txt but isn't.
Shoes.app(title: "File I/0 test",width:800,height:650)  do
    ARGF.each do |line|  #readLine.each has same result
        puts line
        para line
    end
end

In retrospect, I do also see a further difference between Shoes and Ruby: Shoes ALSO prints out the source code of the program I am running, along with any files I pass along. If I try to input a file to standard in, ONLY the source code is printed.

I imagine this means that the shoes app is taking my program as an input, and then not sanitizing (or whatever the correct word would be) the input when it passes it along to my code. This seems to strengthen my "Shoes eats Standard In" hypothesis, since it is clearly USING standard In for something. I guess it can take two files in a row, but not one file and THEN a reference to standard in.

I can confirm that Ruby without Shoes provides identical behavior if I mix file input and STDIN with:

ruby testargf.rb  aus_simple.txt < testargf.rb

Solution

  • I have had some success with the "-" special filename character, which apparently is a signal to switch to STDIN on the command line.

    Example of use:

    shoes testargfshoes.rb -   <aus_simple.txt
    

    Don't pass the "-" without passing any standard In, makes it hang.

    Found the answer here: https://robots.thoughtbot.com/rubys-argf