Hello,
I tried looking for a library that lets me write (lazy) command line pipes
in Ruby but came up short.
I would like to make a function that invokes a executable to transform an
object into text and either saves it to a file or dumps it out.
Maybe using arrays to build up the command is enough;
at least that's all I use.
Ideally, I could write it like this:
def pipeline
return self.to_text >> Process.spawn("dot")
end
Ideally, this pipeline is not invoked until the user does the following:
pipeline.write(path) //writes to file
command = # ... build the command array...
Then maybe something like:
IO.pipe do |r, w|
th = Thread.new { w.write(self.to_text) }
pid = Process.spawn(env, *command, in: r, out: path)
th.join
Process.waitpid2(pid)
end
[*] of course, I'd like to get Thriber accepted to make the above
cheaper with s/Thread/Thriber/: https://bugs.ruby-lang.org/issues/13618
Too late for 2.5, I guess :<
or
pipeline.read //returns String
IO.popen(..., &:read) and `backtick` work well for me with tiny
command outputs; I prefer IO.popen with arrays to avoid
escaping.
But one of the best things about pipes (and IO objects in
general) is doing incremental processing for large outputs or
inputs. RAM is precious to me and I frequently work with
large files, so I prefer to use IO#readpartial or similar.
Unbound IO#read and #gets gives me nightmares :<
Does something like this exist already?
I started working along building complex pipelines as arrays
(and also throwing Procs anywhere in the pipeline) at:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/435624
And eventually for dtas <https://80x24.org/dtas/>:
https://80x24.org/dtas-all/20170428200809.28822-1-e@80x24.org/raw
But haven't really gotten around to doing more along those lines.
···
Samuel Williams <space.ship.traveller@gmail.com> wrote: