0

I'm trying to read from a file, and because my more complex code doesn't work, I came back to basics to see whether it even reads properly. My code:

   MyParser.new(STDIN.read).run.lines.each do |line|
     p line.chomp
   end

I use

ruby program

(it's placed in a bin directory and I saved it without .rb )

Now program is waiting for me to write something. I type:

../examples/file.txt

and use CTRL + Z (I'm on Windows 10). It produces ^Z and I hit enter. Now I have an error:

Invalid argument @ rb_sysopen - ../examples/file.txt (Errno::EINVAL)

MyParser class and its whole logic works fine. I'll be grateful for any hints.

Jes
  • 323
  • 2
  • 18

1 Answers1

0

Without knowing, what your MyParser expect, it is hard to know, what you need.

But maybe this helps:

 MyParser.new(STDIN.gets.strip).run.lines.each do |line|
   p line.chomp
 end

I would extend it by a message what you need:

puts "Please enter the filename"
STDOUT.flush
MyParser.new(STDIN.gets.strip).run.lines.each do |line|
  p line.chomp
end

With STDOUT.flush the user gets the message, before STDIN.getswaits for a message.


In your case I would take a look on ARGV and call the program with:

ruby program ../examples/file.txt

Your program should then use:

MyParser.new(ARGV.first).run.lines.each do |line|
  p line.chomp
end
knut
  • 27,320
  • 6
  • 84
  • 112