Hacker News new | ask | show | jobs
by _zagj 536 days 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).

2 comments

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.
This goes at least as far back as anaphoric macros: https://en.m.wikipedia.org/wiki/Anaphoric_macro.
shame 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.