Issue #13527 has been reported by janko (Janko Marohni).

----------------------------------------
Feature #13527: Accept IO object as stdin data in Open3.capture
https://bugs.ruby-lang.org/issues/13527

* Author: janko (Janko Marohni)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Currently Open3.capture3, Open3.capture2, Open3.capture2e accept a :stdin_data option, which allows you to write a String into subprocess' standard input. This patch adds the ability to also pass in an IO-like object (any object that respond to #read) as :stdin_data, which will them be streamed to standard input.

  Open3.capture3("file", "--mime-type", "--brief", "-", stdin_data: File.open("image.jpg"))
  Open3.capture3("ffprobe", "-print_format", "json", "-i", "pipe:0", stdin_data: File.open("video.mp4"))

This is convenient when you want to pass in files into standard input (images, videos etc), because this way you don't have to load the whole file into memory, the file contents will get efficiently streamed into subprocess' standard input.

Another advantage is that many command line tools will stop reading the standard input once they get enough data. In both the examples above the subprocess will stop reading standard input as soon as it gets the information it needs (the image MIME type or video metadata), and in both examples it turns out to be about 1-2MB. This isn't that useful if the IO object represents a file on the filesystem (where reading is fast), but it becomes very useful when the IO object represents a file from the database or a remote file over HTTP. That way you don't need to guess how much data the subprocess needs, you can just give it the IO object and it will read as much as it needs, and then only that amount will be retrieved from the database or downloaded from the Internet.

---Files--------------------------------
open3-accept-stdin-data-as-io.patch (6.09 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>