We love reading, watching, and listening in order to keep our skills sharp and our perspectives fresh. Here are some of the resources we learned from this month.
The Practical Effects on the GVL on Scaling in Ruby
In "The Practical Effects of the GVL on Scaling in Ruby", Nate Berkopec provides a great practical overview of parallelism and concurrency, along with the challenges they pose to C-based interpreters. The post is a few years old—Ractors are now a reality—but the exposition is well worth a bit of mental fast-forwarding. Short version: We commonly separate tasks into those that are CPU-bound, spending most of their time executing instructions, and those that are I/O bound, spending most of their time waiting for data to be sent or received. In order to make the best use of the C-based Ruby interpreter, you need to assess where your job falls on this spectrum.
extend self in Ruby
Modules are a great way to encapsulate logic that is shared between objects. But what if you want to use that logic directly (e.g.
MyModule.my_method)? Turns out this is not callable unless it’s defined as
def self.my_method. Though doing this would mean other objects that extend
MyModule would no longer be able to call that method; not what we want. If instead
#my_method becomes directly available on
MyModule and remains available on any objects
ActiveRecord Supports Postgres Enum Types
Enums are great for hardening up validations. But reading integer values can be a little cryptic. One solution is to use Postgres's enum types in migrations. And now Rails 7 gives us a power-up with Postgres enumerated types baked into AR.
Sometimes, we don’t know exactly where we want to go or how we want to get there. For times like these, we can reach for handy methods like
redirect_back_or_to. By default, the browser will be redirected back to the referrer. The referrer is determined via a request header (HTTP
Referer (sic)) - which isn’t always available because it is based on browser security settings and user preferences. But even in the event that the header is missing, it’s all gravy because we can provide a fallback location to redirect to instead!
In Ruby 3.2 a simple, native value object will be added to Ruby as a core class. Hello,
Data. While I am not particularly thrilled about the name I am excited about the implementation, as I have found myself frequently reaching for value objects. The
define method will be used to initialize an instance of this class with either positional or keyword arguments. My first thought was that this class sounded eerily similar to a
Structs are mutable, collection-like, and dictionary-like, where
Data are not. The PR for this feature is linked in the title but you can check out the proposal for it here.