#​569 — September 9, 2021

Read on the Web

✍️ I know not all readers are into Rails, but when a glut of interesting Rails news comes along, we've got to cover it :-) Scroll down a bit for the normal Ruby goodness if this is you, especially as Jemma is back with a new Tip of the Week
Peter Cooper, your editor

Ruby Weekly

▶  DHH's Rails 7 Alpha Preview with esbuild and Tailwind CSS — If you’re a fan of David Heinemeier Hansson’s (DHH) screencasts, here’s his latest where he shows off more of what’s coming in Rails 7 with regards to JavaScript integration. It’s too early to call how this will pan out, but everyone seems pretty excited about the possibilities here.

DHH on YouTube

Rails 7 Will Have Three Great Answers to JavaScript in 2021+ — Rails 7 (still due later this year) will have new ways to handle (or not) JavaScript; the 'default' being ES6 import maps and the Hotwire stack. The second way is with a typical bundler with the jsbundling-rails gem, and a third option is SPA+Rails API. So long, Webpacker?

David Heinemeier Hansson

Run Faster and Safer Than Linux with Open Source Unikernels — Deploy Ruby with no ops. Run 2X as fast on Google. Run 3X as fast on AWS and deploy in 10s of seconds using open source unikernels.

NanoVMs sponsor

Getting Ready for Autoloading in Rails 7 — The forthcoming Rails 7 release will represent a milestone for autoloading with Zeitwerk’s ‘classic’ mode (used to help maintain legacy app transitions) going away and initializers will only be able to autoload reloadable constants if wrapped in to_prepare blocks. You may need to prepare your 6.x apps for the changes.

Xavier Noria

Real-Time Stress: AnyCable, K6, WebSockets, and Yabeda — How do you stress/load test WebSockets? The old answer is “very awkwardly”, but there’s new tooling afoot that makes this easier, although it’s heavy on the JavaScript :-)

Evil Martians



Senior Ruby Engineers | Remote US/EMEA | Full-Time — Orbit is building mission control for communities like Kubernetes and CircleCI. Join our remote team that values empathy and the occasional space pun.

Ruby on Rails Engineer (Remote) — Join our distributed team and build high-volume eCommerce applications in a workplace made by developers for developers.

Find Ruby Jobs Through Hired — Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It's free for job-seekers.

📕 Articles & Tutorials

Build an Uptime Monitoring System in Ruby — Subomi builds both a metrics service and a monitoring service on top of Google Cloud’s Cloud Storage and PubSub. The monitoring service listens for config changes and restarts the metrics service, which is a nifty pattern to know.

Subomi Oluwalana

Testing Your Edge Cases — Using a little math to find the scenarios you aren’t covering in your tests, then deciding to either test them or code them out of existence.

Joël Quenneville

Has Your Rails Version Fallen Behind? FastRuby.io Can Help — We can help to get your application to Rails 6.1

FastRuby.io | Rails Upgrade Services sponsor

▶  A Look at Import Maps in Rails 7 — Import maps let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser.

David Kimura

Let's Read Eloquent Ruby, Chapter 3 — Brandon reads Ruby books and shares insights he’s picked up along the way. This chapter focuses mostly on collection types.

Brandon Weaver

Deployment From Scratch, 1st Edition Released — “Deployment” here means a full Rails app with Postgres, Let’s Encrypt certs, WebSockets, and more. There are some other demos too (PostgreSQL cluster!) that you can immediately add to your production knowledge.

Josef Strzibny ebook

▶  Talking with Kasper Timm Hansen from the Rails Core Team
Remote Ruby Podcast

Working With RBS in RubyMine — Static typing fun in a popular IDE.
Natalie Kudanova (JetBrains)

🛠 Code & Tools

Dotenv Validator: A Library to Validate Environment Variables — Provides a way to process commented notations in .env.sample files in order to validate values in the eventual environment variables used by your app.

Ernesto Tagwerker

rover: Simple, Powerful Data Frames for Ruby — Designed for “machine learning and data exploration”, it’s worth clicking through to the Jupyter notebook demo on Binder from the README to see it in action.

Andrew Kane

See Logs from Your Ruby and Ruby on Rails Apps in LogDNA for Free

LogDNA sponsor

Suture: A Gem for Refactoring Legacy Code — We first featured this 5 years ago, and while it hasn’t had a ton of updates since then, it’s been getting some fresh love on social this week.

Test Double

Mutant: Automated Code Reviews via Mutation Testing — Another tool we last mentioned years ago, but which is also still getting frequent updates: “Think of mutant as an expert developer that simplifies your code while making sure that all tests pass.” Keep an eye on the licensing, though.

Markus Schirp

Rainbow: A Gem for Colorizing Printed Text on ANSI Terminals — e.g. puts Rainbow("this is red").red .. also support for backgrounds, underlining, etc.

Marcin Kulik

TelephoneNumber: Global Phone Number Validation Library — Based upon Google’s libphonenumber library. Live demo.


💡 Tip of the Week

Numbered Parameters

We have likely all interacted with Ruby blocks which take parameters. Maybe we've seen something like this:

squared_numbers = (1...10).map { |num| num ** 2 }

From Ruby 2.7 onwards, we have an alternate syntax we can use to express this same statement. Instead of naming parameters (|num| above), Ruby allows us to use numbers prefixed with underscores to refer to the parameters. In the case above, since the block has one parameter, we can use _1 to refer to what was previously num:

squared_numbers = (1...10).map { _1 ** 2 }

If there are multiple parameters, we use numbers sequentially to refer to different parameters as they would have been ordered if they were named. For instance, iterating over a hash, we have both a key (city below) and value (population below) parameter. So this:

city_populations = { "Tokyo" => 37_435_191, 
"Delhi" => 29_399_141, "Shanghai" => 26_317_104 }

city_populations.each { |city, population| 
puts "Population of #{city} is #{population}" }

.. becomes this using numbered parameters:

city_populations.each { puts "Population of #{_1} is #{_2}" }

Use numbered parameters or named parameters

Numbered parameters only work if we don't name the parameters. As soon as we give them a name, we can't then also try access them by number:

squared_numbers = (1...10).map { |num| _1 ** 2 }

# SyntaxError (ordinary parameter is defined)

Style note

Before finishing this tip, it's important to note that while in some cases numbered parameters are simpler, there are also some times numbered parameters can make code harder to read and understand than named parameters. I use numbered parameters for simple blocks, and named parameters as soon as any complicated logic appears in a block.

Be sure to agree with your team on when it's stylistically appropriate to use numbered parameters or named parameters!

This week’s tip was written by Jemma Issroff.