Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting input for system() calls in ruby

Tags:

ruby

system

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?

like image 628
dslh Avatar asked Dec 05 '22 21:12

dslh


2 Answers

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.

like image 139
mu is too short Avatar answered Dec 31 '22 05:12

mu is too short


Something like this perhaps (using open as mu suggested)?

contents = "Hello, World!"
open('|echo', 'w') { puts contents }
like image 39
Abe Voelker Avatar answered Dec 31 '22 05:12

Abe Voelker