#541 — February 25, 2021

Read on the Web

💡 We've had some great feedback about the Tip of the Week so we want to keep it going, and it's back this week without another method that may have passed you by :-) Our feature this week is also essentially a giant pile of tips for using irb.
__
Peter Cooper, editor

Ruby Weekly

The Hidden Gems of Ruby's irb — Lots of good info here, including how IRB handles sessions, how to enable tracing, the new measure command, and how to customize your IRB. Oh, and there’s an easter egg, too…

Valentino Stoll

Opal 1.1: A Ruby to JavaScript Compiler — An interesting way to take Ruby code to the frontend by simply translating it into JavaScript. It’s been around for years but 1.1.0 is the first significant release in some time.

Beynon and Contributors

Redis 6.2 Now Available on RedisGreen — The new release includes several minor commands and new options, and is available to try for free on RedisGreen.

RedisGreen sponsor

Rails Design Patterns: The Big Picture — Very simple examples of patterns that you’re likely to see in Rails (and other frameworks) and which, when used properly, can make your code more maintainable.

Paweł Dąbrowski

Quick Bits

CarrierWave 2.2: File Uploads for Rails, Sinatra and Other Ruby Web Frameworks — The popular library has added libvips support, as well as support for the latest version of RMagick.

CarrierWave

▶  Integrating Ruby and C++ using MRuby and CMake — mruby is an official (Matz works on it!) ‘lightweight’ Ruby implementation designed to be linked and embedded with other applications. This 20-minute screencast does a good job of showing how it can be built, integrated and used with a basic C++ program as there are quite a few moving parts to consider.

Kota Weaver

💻 Jobs

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

Nebulab

Ruby/Rails Developer (Remote Friendly) in Beautiful Norway — We build startups and do GOOD tech. A digital democracy startup, a renewable energy startup, a health startup. GraphQL, React. <3

Rubynor

Find Your Next Job 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.

Hired

📘 Articles & Tutorials

Why to Not Use the macOS System Version of Ruby — Daniel is well known for his Ruby and Rails installation guides and while he’s long advised “don’t use the system Ruby” he wanted to really get into detail about why this advice exists.

Daniel Kehoe

Building a Redis-Based Rate Limiter — How a developer approached using Redis to avoid reaching the rate limit of an API with minimal changes to the existing code.

Michał Musialik

Creating an iOS Share Extension for a Turbo Rails App — As the impact of Turbo and Hotwire spreads, we’ll get more tutorials like this that show how quickly we can bridge across web and native landscapes. This could get exciting.

Julian Rubisch

Complete Peace of Mind Rails Hosting — If you need 100% peace of mind Rails hosting, OpsCare is for you. We keep your application online and performing, 24 hours a day, 7 days a week.

OpsCare by reinteractive sponsor

Code Loaders in Ruby: Understanding Zeitwerk — Zeitwerk is the default loader in Rails 6.0, but you can easily use it in any framework or application. Here’s how.

Subomi Oluwalana

Querying PaperTrail Object Changes in JSONPaperTrail is a library that tracks changes made to your models and lets you answer the question “what series of events conspired to put my database in this state?” That’s useful enough on its own, but here we see how such changes are stored.

Kevin Murphy

Simplifying Tests by Extracting Side-Effects — Test-driven development, object-oriented design, and functional programming converge on some similar ideas..

Joël Quenneville

Search and Debug Gems with bundle open — Incredibly useful once you know about it.

Matt Swanson

🛠 Code and Tools

ParallelTests 3.5: More Cores Equals Faster Tests — This week’s 3.5 release adds support for specifying how isolated processes run tests with the specify-groups option (so you can have multiple specs run in multiple processes in a specific formation).

Michael Grosser

Page Title Helper 5.0: Internationalized and DRY Page Titles and Headings for Rails5.0 adds Ruby 3 and Rails 6.1 support.

Lukas Westermann

Rswag 2.4: Seamlessly Adds a Swagger to Rails-Based APIs — Rswag extends rspec-rails “request specs” with a Swagger-based DSL for describing and testing API operations.

Myers, Morris, et al.

Scout APM - Leading Edge Performance Monitoring Starting at $39/Month

Scout APM sponsor

Tree-Sitter: A Parser Generator Tool and Incremental Parsing Library — It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited. General enough to parse any language and claims to be fast enough “to parse on every keystroke in a text editor.” Also has bindings to be used from Node, Python, Ruby, Rust, and others. GitHub repo.

Max Brunsfeld

ActiveRecord-Import 1.0.8: Bulk Data Insertion with ActiveRecord — Does some clever things like generate the minimal amount of SQL inserts by analyzing associations.

Zach Dennis

💡 Tip of the Week

Enumerable#filter_map

Enumerable#filter_map does exactly what its name implies: it filters on certain values and then maps over those values. Let's look at an example of what we might have written before Ruby 2.7 introduced Enumerable#filter_map. In this example, we have an array of a few consecutive integers, and would like to calculate the squares of only the even integers:

[1,2,3,4,5,6].select { |i| i.even? }.map { |i| i ** 2 }
=> [4,16,36]

Enumerable#filter_map does both of these commands together for us! The block we need to pass to Enumerable#filter_map is a little different than the above two blocks. Enumerable#filter_map will return any non-nil results of executing a block.

It is straightforward to understand the syntax of Enumerable#filter_map if we instead think of it as doing the equivalent Enumerable#map and then Enumberable#compact operation. Let's take a look at how we might obtain the same output as above with a map and compact:

[1,2,3,4,5,6].map { |x| x ** 2 if x.even? }.compact
=> [4,16,36]

We can use the exact same block as the map and compact with our new method, Enumberable#filter_map:

[1,2,3,4,5,6].filter_map { |x| x ** 2 if x.even? }
=> [4,16,36]

Alas, Enumerable#filter_map has turned two method calls into one! It is slick, and slightly easier than writing a select and map, or a map and compact.

This week’s tip was written by Jemma Issroff.