ryanmjacobs
Joined 11 years ago
Karma
582
Submissions
156
Member since
Jan 31
notryan.com
webfpga.io
View on Hacker NewsJoined 11 years ago
I use it for code like this:
raw = IO.popen(%W[gzip -d -c -- #{path}]){_1.read}
raw = IO.popen(%W[gzip -d -c], in: pipe){_1.read}
raw = IO.popen(%W[git status --porcelain -- #{path}]){_1.read}
Useful because it eliminates /bin/sh footguns (e.g. `md5sum hello&world` forking or `~john` expanding), plus you get kernel.spawn() options. `open("| ...)` only made sense for the second example.Anyway, I find "_1" more eye-pleasing than:
IO.popen(%W[gzip -d -c #{path}]){|io| io.read} pp open("|ls -lh /usr/bin/ls"){_1.read}
"-rwxr-xr-x 1 root root 135K Aug 30 04:57 /usr/bin/ls\n"
or to quickly print tabular data open("|column -t -s \\t", "w"){_1 << tsv_data}I'd like know if `it` is merely an alias for `_1`, or if protects from this "arity issue" too.
Tempfile.create(anonymous: true) removes the created temporary file
immediately. So applications don’t need to remove the file.
[Feature #20497]
I use a similar pattern a lot: file = Tempfile.new.tap(&:unlink)
file << "... some really large data to pipe"
system("md5sum", in: file.tap(&:rewind))
It's a neat trick to leverage the filesystem for large amounts of data (e.g. "psql \copy" generation), without littering tempfiles everywhere. Once the last fd disappears, the filesystem releases the data; so you don't have to "trap" signals and add cleanup routines. (Hint: you can also use these unlinked-tempfiles for command output, e.g. huge grep output, etc.)On Linux systems, `open(..., O_TMPFILE)` is typically used, which provides additional safety. The created file is initially unnamed (no race condition between `open` and `unlink`).
When I needed safety, my non-portable solution was to use Ruby's syscall feature. (Btw, I love that you can do this.)
require "fcntl"
SYS_OPEN = 2
O_TMPFILE = 0x00410000
O_RDWR = Fcntl::O_RDWR
def tmpfile
mode = O_RDWR | O_TMPFILE
fd = syscall(SYS_OPEN, "/dev/shm", mode, 0644)
IO.for_fd(fd)
end
But... Another pleasant surprise from the PR (https://bugs.ruby-lang.org/issues/20497). Linux 3.11 has O_TMPFILE to create an unnamed file.
The current implementation uses it.
Excellent to see :)
Makes the PR even better!Doing taxes right now and I spent about 10 minutes manually exporting+emailing OFX/QFX files to myself for Quicken. Kind of a pain. Every other card I have supports automatic data pulling.
Probably will move my main card to something that has automatic integration.