8 ms·
Ruby 3.4 Highlights
- deleted 2y ago[deleted]
- _zagj 2y ago> In Ruby 3.4, it has been added as a default name for the first parameter passed to a block. Rather than specifying a name in trivial cases like the one above, you can now write: > [ > "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).
- jshmrsn 2y agoI am familiar with ‘it’ as a default closure input from Kotlin. From a quick search, that in turns seems to be inspired by Groovy.
- alex_muscar 2y agoThis goes at least as far back as anaphoric macros: https://en.m.wikipedia.org/wiki/Anaphoric_macro https://en.m.wikipedia.org/wiki/Anaphoric_macro.
- inopinatus 2y agoshame about that example, since 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.
- Trasmatta 2y agoThe default block parameter name seems small, but I feel like I'm gonna use it all the time, especially when I'm in the Rails console just trying to debug data
- lucasoshiro 2y agoKotlin also has it, I can tell you that it's really useful
- cempaka 2y agoRuby has had this in the same form as "_1" for a while (and the obvious other indexes in the case of multiple params), but I agree Kotlin's "it" reads much better.
- onedognight 2y agoGroovy has had “it” for a while. I suspect it predates Kotlin’s usage.
- pledg 2y agoAlso matches Groovy
- jarjoura 2y agoI constantly run into situations where I need to nest an iterator computation, and things like "it" get confusing. 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.
- Trasmatta 2y agoI doubt I'll use it in committed code very often at all, this feels more like something that's very useful when hacking at something in the REPL
- deleted 2y ago[deleted]
- elif 2y agonested_example = [1, 2, 3, 4, 5].map do [it, (1..it).map { it * it }] end Has 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 }] end Seems like a mental over complication of a non-issue to me.
- baggy_trough 2y agoHappy to see chilled strings; eventually getting rid of the frozen strings pragma will be enjoyable.
- jaynetics 2y agoYou can already get rid of it with the freezolite gem. https://github.com/ruby-next/freezolite https://github.com/ruby-next/freezolite
- asicsp 2y agoSee also: https://nithinbekal.com/posts/ruby-3-4/ https://nithinbekal.com/posts/ruby-3-4/
- torben-friis 2y agoI happen to be reading the latest edition of the pickaxe book, as I'll soon be using Ruby at work (first experience) and this 'it' update sounds so much better than the _1 described in the book. I remember thinking it clashed a bit with the idea of trying to make the code read like natural language.
- ksec 2y agoDid you know you were going to use Ruby before Applying? Just wondering.
- torben-friis 2y agoYup. I don't mind switching languages, I've done it a few times and I enjoy learning new things. 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?
- ksec 2y ago>Why, is there any issue with the choice I'm not aware of? No, not at all. I was just interested since there is a sudden influx of people joining Ruby ( I guess mostly Rails ) companies without previous Ruby background. As I have notice this across HN, Reddit, Twitter and elsewhere. And yes Ruby market tends to be on the slightly higher end because they mostly hire people with years of programming experiences and lack of junior position. And there used to be complains about not being able to find people with Ruby / Rails experiences etc. So it is a good market shift I guess.
- torben-friis 2y agoAh well, as soon as I even glanced at Ruby my social media feeds were filled with tech influencers. There is a niche there of very popular channels producing Ruby tutorials, seemingly aimed at the junior js-bootcamp dev crowd. They also focus on Neovim, tmux and other terminal based tools. It is a completely anecdotal observation, but my guess is that it might explain part of the trend.
- behnamoh 2y agoIs ruby used outside of web dev (i.e., Ruby on Rails)? it seems like a nice cute language but I almost never hear about it.
- cempaka 2y agoAt my day job we use Ruby with Sorbet to stitch together a large number of data engineering pipelines. It's a good choice since we also often stand up small Rails apps to expose the results from those pipelines, so we can keep everything in one language. The fluent functional approach it enables is pretty nice for that business logic (but I would not want to do it without the gradual typing on top). We do find ourselves glancing over at the Python ecosystem with envy from time to time but overall it has worked pretty well. As I've gotten better with it I find myself reaching for irb for more and more one-off tasks in the shell too.
- Sinjo 2y agoOh nice! I haven't used Sorbet yet, but this is the second time I've heard good things about it outside of Stripe (where it originated). I'll have to give it a proper look.
- lucasoshiro 2y agoI think it's quite sad that Ruby is so associated with Rails. Ruby is one of my favorite languages and last time that I touched a Rails code was in 2017. Even in web dev Rails is not the only thing in Ruby, there's also Sinatra which is an elegant micro-framework for building HTTP APIs, similar to Flask. 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://news.ycombinator.com/item?id=40763640
- Mystery-Machine 2y agoThe author also forgot to mention that the new error output doesn't mix ` and ' anymore. 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...
- mostlysimilar 2y agoThis has bothered me for decades, why was this ever the case to begin with?
- BalinKing 2y agoI'd presume it's the same motivation behind quotes in TeX (`foo' and ``bar'')—maybe typewriters didn't use to have true curly quotes, or something? EDIT: https://en.wikipedia.org/wiki/Backtick#As_surrogate_of_apostrophe_or_(opening)_single_quote https://en.wikipedia.org/wiki/Backtick#As_surrogate_of_apost... seems relevant
- thwarted 2y agoIs the stack trace in the section "Clearer exception backtraces" actually from 3.4? It has mixed backticks and single quotes.
- Sinjo 2y agoFixed! I grabbed that example from the bug tracker issue where the change was discussed and that wasn’t based on a version where the backtick change had been made.
- tiffanyh 2y agoI know Shopify has a great site on comparing Ruby vs YJIT perf. https://speed.yjit.org https://speed.yjit.org But does anyone have current numbers on how Ruby/YJIT compares to something like Python/PHP/LuaJIT?
- pilaf 2y agoThe Computer Language Benchmarks Game has some compared benchmarks: Ruby+YJIT vs Python: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/ruby-python3.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Ruby+YJIT vs PHP: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/ruby-php.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Ruby+YJIT vs Lua: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/ruby-lua.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- igouy 2y agoAnd for an appearance of comparability the "#8" programs are un-optimised single-thread transliterated lowest-common-denominator style into different programming languages from the same original. https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/comparable.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- hit8run 2y agoI have a love-hate relationship with Ruby. On one hand, it's a very expressive language that's excellent for building structured, lightweight applications. On the other hand, it requires significant discipline and a strong memory of your codebase and all the different types in play. When you combine this highly dynamic nature with Rails, especially its concerns and hooks, it can become extremely challenging to understand what's actually happening in the system.
- ryanmjacobs 2y agoI'm pleasantly surprised to see this improvement: 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 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!
- 15155 2y agoThis is available in older Ruby versions by using the upstream `tempfile` gem version.
- magic_smoke_ee 2y agoI don't understand the change of the default block parameter name. It's already _1, _2, etc. Why create yet another, backward-incompatible way? 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.
- allknowingfrog 2y agoI agree that `it` is unnecessary, but I'm really confused by the push for a type system. It adds a lot of clutter to a language that optimizes for expressiveness. I would prefer to work in something like Go, which is designed to have types, rather than awkwardly patch them into Ruby.
- magic_smoke_ee 2y agoApples (dynamic language) vs. oranges (static languages). Python's gradual typing approach solved this problem similarly to TypeScript, which is the right way to do it: https://peps.python.org/pep-0483/ https://peps.python.org/pep-0483/
- allknowingfrog 2y agoIt may be less bad, but I don't think there's a right way to retroactively add a type system to a dynamic language. I generally like working in JavaScript, but I have no desire to ever see TypeScript again.