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!Also damn Nim needs to do a much better job with their docs and site. `nim result` returns this very outdated third-party link first and nothing else official.
I'm all for adding language features to avoid boilerplate, and it's clearly useful. I just want to call out that anonymous typing can be polarizing in large codebases and maybe only use it sparingly.
[it, (1..it).map { it * it }]
endHas an ambiguity, so you just add |x| to one of them..
nested_example = [1, 2, 3, 4, 5].map do |x|
[x, (1..x).map { x * it }]
endSeems like a mental over complication of a non-issue to me.
(This is terrible please don’t)
items.map { foo(_1) }
items.map { foo(it) }But does anyone have current numbers on how Ruby/YJIT compares to something like Python/PHP/LuaJIT?
Ruby+YJIT vs Python: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
Ruby+YJIT vs PHP: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
Ruby+YJIT vs Lua: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
> [
> "beige chinos",
> "blue jorts",
> "rainbow jorts",
> ].filter { it =~ /jorts/ }
> # => ["blue jorts", "rainbow jorts"]
This reminds me of Perl's $_ (which in Ruby is last line read from STDIN).
ary.grep /(j|sh)orts/
already exists, and therefore sells the standard library short. try this: terms = %w(foo? bar q**x)
Regexp.new "\\b(#{terms.map { Regexp.escape it }.join ?|})\\b"
#=> /\b(foo\?|bar|q\*\*x)\b/
and observe that it's at the margins of instant comprehension where syntax shorthands like "it" add value.Ruby 3.3
> 'asd' + 1 (sitar-report):1:in `+': no implicit conversion ...
Ruby 3.4
> 'asd' + 1 (sitar-report):1:in 'String#+': no implicit conversion...
EDIT: https://en.wikipedia.org/wiki/Backtick#As_surrogate_of_apost... seems relevant
I remember thinking it clashed a bit with the idea of trying to make the code read like natural language.
It's an unusually half-baked feature by Ruby's standards. I think there was some hesitation about the name "it" initially, because "it" is an essential method name in rspec, and is used within blocks there.
I'd like know if `it` is merely an alias for `_1`, or if protects from this "arity issue" too.
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}The market for ruby seems to have good salaries and job satisfaction despite being smallish, so it didn't seem like a bad area to get some experience in.
Why, is there any issue with the choice I'm not aware of?
And still, they doom march forward with the fragmentation of rbs and sorbet/tapioca, rather than adopting in-band gradual typing like Python did.
I think the greater problem is Ruby appears to be developed by a nearly-closed small clique of people who refuse to listen to others or to reason.
Some projects that I remember that uses Ruby that are not related to web dev: Homebrew, Metasploit, Vagrant and Jekyll.
I also find Ruby very useful for shell scripts. I wrote a blog post about that some months ago, you can read it and the discussion about it here: https://news.ycombinator.com/item?id=40763640
https://www.amazon.co.jp/dp/4774176435
Before Ruby, my preferred fun language was Perl. Ruby is like Perl but without having to type $ all the time. I have never used Rails.
I think the reason it's not talked about a lot is mainly because Rails overshadowing it and because there aren't so much hype or controversies around it.
It's actually the language of choice for a lot of OpenSUSE projects as well, like the Open Build Service.
The reason you don't here much about it is because of its maturity.
However, Ruby doesn't make sense to me as a general applications language (Swift & Kotlin have won me over from C++ & Java), or as a systems programming language (I'm still rusted on to C, pun intended), and I remain super ambivalent about Rails despite using it extensively to stick web interfaces onto things. That doubt is in large part because Rails frequently undermines the Smalltalk-ish heart of Ruby.