Wednesday, 15 August 2012

ruby - Efficiently pipe in-memory file contents to a command line command -



ruby - Efficiently pipe in-memory file contents to a command line command -

i have ruby script that, @ 1 point, has in-memory file may or may not backed entry in filesystem. naive solution create tempfile if filesystem entry not exist, result in command line command re-reading file memory.

ideally, avoid reading file memory more 1 time since potentially quite large.

now, command line command take piped input, thought might solution, cannot find way accomplish piping ruby file object's contents happening on command line.

i'm open other recommendations if i'm coming @ wrong direction. files not backed filesystem entry beingness read remote http stream.

one way read ios contents string, , utilize kernel#open (with |), io::popen or open3 create subprocess , write contents subprocesses stdin:

f = the_file_or_io_object info = f.read io::popen('the_command', 'r+') |io| io.write info io.close_write puts io.read end

although avoids going writing file disk (unless e.g. tempfile) involves reading file contents memory , passing them subprocess, in memory twice. if want avoid utilize fork (if scheme has it) , reopen:

# f before, no need read in time pid = fork $stdin.reopen f # stdin file, when command run see # on stdin exec 'the_command' end process.wait pid

if you’re on windows won’t have fork, seek spawn, redirecting stdin:

pid = spawn 'the_command', :in => f process.wait pid

ruby linux command-line

No comments:

Post a Comment