Today I Learned

hashrocket A Hashrocket project

Numbered Parameters to Blocks in Ruby

Ruby blocks offer a shorthand to positional block variables.

Normally you'd see a block like this:

[1, 2, 3].each { |number| do_something(number) }

However with Numbered Parameters, introduced in Ruby 2.7, we can express this like so:

[1, 2, 3].each { do_something(_1) }

The _1 takes the place of an explicitly defined block variable.

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.