💡 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 | |
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 |
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 |
▶ 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 |
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 |
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 |
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 |
Querying PaperTrail Object Changes in JSON— PaperTrail 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 |
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 |
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 |
💡 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. |
|
|