4 ms·
> 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,
by _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.