Gnarly Learnings from October 2022
Tags: learnin, ruby, rails, javascript
Published: 2022-11-04 14:37:32

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](https://www.speedshop.co/2020/05/11/the-ruby-gvl-and-scaling.html)

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](https://www.bigbinary.com/blog/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 `MyModule` `extend`s itself, `#my_method` becomes directly available on `MyModule` and remains available on any objects extending `MyModule`!

##[ActiveRecord Supports Postgres Enum Types](https://guides.rubyonrails.org/active_record_postgresql.html#enumerated-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](https://www.postgresql.org/docs/current/datatype-enum.html) in migrations. And now Rails 7 gives us a power-up with [Postgres enumerated types baked into AR](https://guides.rubyonrails.org/active_record_postgresql.html#enumerated-types).

##[Web Almanac: JavaScript in 2022](https://almanac.httparchive.org/en/2022/javascript#introduction)

The Web Almanac, unlike the Farmer’s Almanac, won’t tell you when winter begins or how long after the last full moon in October you should plant your garlic, but it does provide insight into the state of the web. This section focuses specifically on JavaScript, its trends, and usages but there are other goodies in the almanac too - definitely take the time to check out the other sections on CSS, UX, markup, and more!

## [`redirect_back_or_to`](https://api.rubyonrails.org/v7.0.4/classes/ActionController/Redirecting.html#method-i-redirect_back_or_to)

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!

## [`Data.define`](https://github.com/ruby/ruby/pull/6353)

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 `Struct`. However, `Struct`s 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](https://bugs.ruby-lang.org/issues/16122).

## Contributors
- [Erik Cameron](https://github.com/erikcameron)
- [Ethan Fertsch](https://github.com/efertsch)
- [Taylor Kearns](https://github.com/taylorkearns)
- [Will Mahoney](https://github.com/WMahoney09)

_Learn more about how The Gnar [builds software](https://www.thegnar.com/software-development)._