[HN Gopher] Ruby 3.4 Highlights
       ___________________________________________________________________
        
       Ruby 3.4 Highlights
        
       Author : mscccc
       Score  : 149 points
       Date   : 2025-01-01 14:18 UTC (8 hours ago)
        
 (HTM) web link (blog.sinjakli.co.uk)
 (TXT) w3m dump (blog.sinjakli.co.uk)
        
       | anonnon wrote:
       | > 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 wrote:
         | I 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 wrote:
           | This goes at least as far back as anaphoric macros:
           | https://en.m.wikipedia.org/wiki/Anaphoric_macro.
        
       | Trasmatta wrote:
       | The 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 wrote:
         | Kotlin also has it, I can tell you that it's really useful
        
           | cempaka wrote:
           | Ruby 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 wrote:
             | Groovy has had "it" for a while. I suspect it predates
             | Kotlin's usage.
        
           | pledg wrote:
           | Also matches Groovy
        
         | jarjoura wrote:
         | I 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 wrote:
           | I 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
        
           | elif wrote:
           | nested_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.
        
             | snuxoll wrote:
             | Implicit `it` shadows in other languages like Kotlin just
             | like any other variable scope, and I really can't say I've
             | ever had it be a problem (implicit receivers, on the other
             | hand, can be a right pain in the ass in poorly designed
             | libraries and can I just say Gradle can go right to hell
             | for it).
        
             | jarjoura wrote:
             | It's a nonissue in small projects, but I'm not sure I agree
             | that `{ x * it }` is easy to reason about when coming back
             | to this block in 6 months. It's mostly something that I've
             | seen bite engineers during refactoring.
        
         | Spivak wrote:
         | I was disappointed that "block parameter name" wasn't the
         | ability to give a block an actual name that will show up in a
         | stack trace. It's been an endless source of frustration when
         | your stack trace is just a bunch of anonymous blocks and procs.
         | I assume JS devs feel a similar pain with anonymous functions.
        
         | sergiotapia wrote:
         | Similar ish but equally cool is Nim's `result` keyword for
         | functions. https://nim-by-example.github.io/variables/result/
         | 
         | 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.
        
         | regularfry wrote:
         | I've not been tracking ruby core discussions for ages now, but
         | I could have _sworn_ that exactly this was discussed and
         | rejected a decade or so ago.
        
       | baggy_trough wrote:
       | Happy to see chilled strings; eventually getting rid of the
       | frozen strings pragma will be enjoyable.
        
         | jaynetics wrote:
         | You can already get rid of it with the freezolite gem.
         | https://github.com/ruby-next/freezolite
        
       | asicsp wrote:
       | See also: https://nithinbekal.com/posts/ruby-3-4/
        
       | kace91 wrote:
       | I 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 wrote:
         | Did you know you were going to use Ruby before Applying? Just
         | wondering.
        
           | kace91 wrote:
           | Yup. 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 wrote:
             | >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.
        
               | kace91 wrote:
               | Ah 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.
        
         | jaynetics wrote:
         | _1 is also a bit of a footgun. It becomes an array of all block
         | params IF the block has an arity > 1 AND no other block param
         | (e.g. _2) is referenced anywhere within the block.
         | 
         | 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.
        
           | ch4s3 wrote:
           | I pity anyone needing to debug a fiddly Rspec problem in the
           | next few years.
        
             | ezekg wrote:
             | IIRC, the decision was that it in a block is only ever
             | called without arguments, e.g. map { it * 2 }, and it in
             | Rspec is only ever called with arguments, e.g. it
             | "describes a test" do ... end. So it ended up being a non-
             | issue.
        
       | behnamoh wrote:
       | Is 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 wrote:
         | At 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 wrote:
           | Oh 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.
        
             | cempaka wrote:
             | I think they direct people to tapioca by default at this
             | point, but that was key to making it a smooth process for
             | us.
        
         | lucasoshiro wrote:
         | I 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
        
           | Levitating wrote:
           | Congratulations on getting Matz to tweet about your post.
        
             | lucasoshiro wrote:
             | Thanks! I never thought this would reach him!
        
           | watercolorblind wrote:
           | Some other examples: DragonRuby, and Chef (which I prefer
           | over ansible).
        
           | UncleOxidant wrote:
           | I got into Ruby kind of early, around 2001. When Rails
           | initially came out a few years later (circa 2004, IIRC) I
           | remember being excited to see something putting Ruby out
           | there giving it some publicity. But in a few years I think a
           | lot of us were regretting the fact that most people's view of
           | Ruby was formed by Rails.
        
         | Trasmatta wrote:
         | Definitely, although Rails is its most common use
        
         | petre wrote:
         | Yup, I wrote smartcard reader programs in it. Some of them talk
         | to a http server using async-http. It can be used for anything
         | that Perl or Python is good at, well maybe except data science
         | because Python really shines there. And irb is massively useful
         | for everyday tasks.
        
           | cempaka wrote:
           | I wish I could reach for Kotlin as a kind of "Ruby with
           | typing out of the box" but the lack of a good REPL is a real
           | dealbreaker.
        
             | Alifatisk wrote:
             | You'll get Ruby with typings soon when rbs-inline is
             | finalized
        
               | Sinjo wrote:
               | Ooh, I hadn't heard of rbs-inline before. I was always a
               | bit put off by the type definitions having to live in
               | separate files.
        
               | cempaka wrote:
               | Is that really something that's been slated to be added
               | to the language proper? I attended RubyConf and in his
               | keynote Matz seemed pretty opposed to adding types to
               | Ruby source, and argued in favor of automatic typing
               | instead.
        
               | petre wrote:
               | You can always use Crystal or Dart if you want builtin
               | types. I like Ruby the way it is an trust Matz with his
               | language design decisions.
        
               | cempaka wrote:
               | Yeah I actually did not necessarily mean to dispute
               | Matz's perspective there. I actually think it will
               | benefit programming as a whole if at least one widely
               | used language takes the path of trying to eschew explicit
               | type annotations.
        
               | Alifatisk wrote:
               | Crystal is the closest alternative to Ruby with types we
               | have, even though they are semantically different and
               | differ in some places. I do not see why you mentioned
               | Dart as an alternative thought, it's a totally different
               | language.
        
         | Toutouxc wrote:
         | Yeah, I use it for everything between 5-lines-of-bash and a
         | needs-a-compiled-language. You almost never hear about it,
         | because it's mature. No big controversies, no huge fuckups or
         | new versions that break everything, just a steady trickle of
         | improvements.
        
           | AnthonBerg wrote:
           | I am happy in this moment because as I lay here with my ear
           | on the ground, I can hear many many people hearing about Ruby
        
         | baggy_trough wrote:
         | Sure. I use it for almost everything, because it's so pleasant
         | to use.
        
         | Levitating wrote:
         | Yes! In practice it's a scripting language similar in vein to
         | Python, and it's used for similar things.
         | 
         | 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.
        
         | cess11 wrote:
         | You might have heard about Metasploit.
        
         | Alifatisk wrote:
         | Oh yes, Ruby is my go to language to use when I need to glue
         | things together or when I want to automate tasks on my
         | computer. It's such a pleasant language to use!
         | 
         | 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.
        
         | omoikane wrote:
         | Ruby is my go to language for short "fun" programs, as there
         | are many places where it optimized for development experience
         | (such as the new "it" feature). One of the committers, Yusuke
         | Endoh, clearly understood the joy of coding in itself as
         | opposed to being means to an end, and has authored a book on
         | esoteric programming:
         | 
         | 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.
        
         | segfaltnh wrote:
         | We use it for microservices that only deal with Json and avro.
         | Of course, we do that on Rails for some reason I'll never
         | understand.
        
         | thayne wrote:
         | It's used for chef/cinc. I rather like that "recipes" use ruby
         | with an edsl rather than yaml like ansible or salt.
        
       | Mystery-Machine wrote:
       | The 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 wrote:
         | This has bothered me for decades, why was this ever the case to
         | begin with?
        
           | BalinKing wrote:
           | I'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_
           | apost... seems relevant
        
         | thwarted wrote:
         | Is the stack trace in the section "Clearer exception
         | backtraces" actually from 3.4? It has mixed backticks and
         | single quotes.
        
           | Sinjo wrote:
           | Fixed! 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 wrote:
       | I know Shopify has a great site on comparing Ruby vs YJIT perf.
       | 
       | https://speed.yjit.org
       | 
       | But does anyone have current numbers on how Ruby/YJIT compares to
       | something like Python/PHP/LuaJIT?
        
       ___________________________________________________________________
       (page generated 2025-01-01 23:00 UTC)