I'm trying to download a file using net/sftp and pass its contents as the stdin for a command-line app. I can do it by first writing the file to disk but I'd rather avoid that step.
Is there any way to control the input to a program invoked with system()
in ruby?
Don't use system
at all for this sort of thing, system
is best for running an external command that you don't need to talk to.
Use Open3.open3
or Open3.open2
to open up some pipes to your external process then write to the stdin
pipe just like writing to any other IO channel; if there is any output to deal with, then you can read it straight from the stdout
pipe just like reading from any other input IO channel.
Something like this perhaps (using open as mu suggested)?
contents = "Hello, World!"
open('|echo', 'w') { puts contents }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With