🤝 Short Ruby - edition #17

Briefly about everything happening in Ruby world

If you want a short excerpt of this newsletter, I created one here

This edition was created with support from @adrianthedev from Avo for Ruby on Rails (a friendly full-featured Rails admin panel), @jcsrb, and @kpumuk, who sent me recommendations to include in the newsletter.

about the community

Linus Torvalds talks about Git's success in an interview:

I listened to the interview, and I want to make this clear: What Linus is saying is that Git reached success quicker than he expected, thanks to Ruby developers. In this context, “strange” is used somehow, like saying strange people that understood what I created.

Collin Jilbert shared lovely words celebrating his 4th year of writing code:

👉 Kirill Shevchenko shared in a thread multiple examples of using Ruby predefined global variables:

uby has many pre-defined global variables. There are 5 of them you might find useful: $!, $$, $0, $@, $* $! contains the last raised exception in the current call stack.

Here is a summary of the code samples shared by Kirill in the thread:

You should read the thread as more people shared how they use these variables. Also, if you are curious about what global variables Ruby has, check out the documentation about Pre-defined global variables.

If you don’t want to work with the $ notation, there is a module (now part of stdlib) called “English” (seems to be since Ruby 3.0):

👉 Adrian Marin shared a thread about how to move from Heroku to Dokku:

Today I moved Avo's demo apps from Heroku to a @dokku instance. Here's what I did 🧵👇

👉 David Copeland shared a thread where they build a good DX for running bin/setup:

You can find the scripts for doing this setup on Dave Github. It is worth considering this brief summary of what makes a good bin/setup script:

bin/setup needs to have a few properties: • does the setup • is idempotent/safe to run any time • is clear what it's doing & where things go wrong • handles and reports useful errors • relis only on stdlib not gems because it installs them! 3/

👉 David Teren shared a code sample showing how to configure Rails 7 (Hotwire) and Devise logout:

A simple solution for Rails 7 (Hotwire) & Devise sessions destroy.

👉 Aaron Patterson shared a PR that was merged to Ruby about changing how Classes and Module are storing instance variables:

Today @jhawthorn merged a PR that changes Classes and Modules to use object shapes for storing instance variables rather than a hash table. Class IVAR reads are 2x faster, and it saved 16MB RAM! 👏

👉 Greg Navis shared a thread explaining some Active Record methods that can be useful when dealing with persistence:

💡 Rails tip: Active Record implements a bunch of predicate methods that can come in handy at times 1️⃣ previously_new_record? 2️⃣ previously_persisted? 3️⃣ destroyed? 4️⃣ new_record? 5️⃣ persisted? ⬇️ Let's have a look at each one of them.

If any of these methods are interesting, you should read the entire thread, as it has code samples for each method. Here is a summary of them:

  • previously_new_record?“Returns true if this object was just created – that is, prior to the last save, the object didn't exist in the database and new_record? would have returned true”

  • previously_persisted?“Returns true if this object was previously persisted but now it has been deleted”

  • destroyed?“Returns true if this object has been destroyed, otherwise returns false.”

  • new_record?“Returns true if this object hasn't been saved yet – that is, a record for the object doesn't exist in the database yet; otherwise, returns false”

  • persisted?“Indicates if the model is persisted. Default is false.”

👉 Kevin Newton shared a code sample on Github showing how to evolve the code when using pattern matching and decide what to execute based on object type:

I keep seeing places in Ruby where I want the pattern for an object to be defined on the object itself, and not in the callsite where it's being matched. Maybe I should just be overriding the=== operator.

Here is a small sample of the code there, but you should read the entire code as Kevin wrote there a good story about how the code evolved:

👉 Benoit Daloze warned about the grpc Ruby gem:

If you are thinking about using the grpc Ruby gem, think twice about it. It's currently basically unmaintained. Here are two important PRs ready to be merged but waiting for months: https://t.co/GVlUGipX0C https://t.co/1qhG03c82E even though many people are waiting the 2nd fix.

👉 Dima Fatko shared a new gem sidekiq-iteration that makes long-running Sidekiq jobs interruptible and resumable:

Introducing sidekiq-iteration (https://t.co/ir7cKKErUK) a new gem that helps to make your long-running sidekiq jobs interruptible and resumable by design. For those familiar with job-iteration from Shopify, this is an adoption of that gem to work with raw Sidekiq (no ActiveJob). https://t.co/1GVza2iYBV
2022-11-02 17:51:57 UTC Hey #Ruby Juniors! @dpaola2 is about to open up the next batch of @SierraRails Junior Developer Program. You can apply here:

👉 Ryan Bates shared asked a question about changing directory structure in Rails:

Has anyone tried changing their Rails app directory structure to vertical slices (feature based) instead of horizontal layers? Any success?

Among the answers:

This suggestion about how to organize could also be worth considering:

👉 Jared White shared that the Rails readme already specifies where to put the business logic:

Every time I hear of a discussion of “where do I put my business logic in my Rails app” I wonder why we even need to discuss this. 😅 The answer is right in the Rails readme! Like, right there! In clear and unambiguous terms!

Here is the relevant quote from Rails docs:

A brief version of this is also present in the official Ruby on Rails guide:

I suggest you read the entire conversation related to Jared's share. It has some great points about how to organize code and also a good debate about naming.

👉 Joel Drapper shared about the future of Phlex and supporting multi-format:

Phlex is going multi-format with dedicated builders for HTML, RSS, ATOM, SVG and JSON. They’ll all use the same basic format — a Ruby class with a template method and optional initialiser — and they’ll all benefit from boot-time compilation.

👉 Greg Navis shared a thread about how Active Record id attribute works:

1️⃣ Active Record implements primary key handling in a dedicated module: github.com/rails/rails/bl… It's clear the id method reads from the column stored in the primary_key attribute. This conversion happens in other places, too.

👉 CJ Avilla shared about the source of using underscores:

I've always wondered where that convention came from. We see it in Ruby with __FILE__, in python its everywhere and how you override methods on objects __str__, but where did this start?

👉 Akshay shared about self keyword in Ruby:

Ruby's self keyword can be pretty confusing to understand, especially if you're new to Ruby. Not understanding it often leads to subtle bugs that can be difficult to debug. Here're the different forms it can take, depending on the context in which it's used 👇

And here, Akshay shared a very good summary:

Here are four rules to keep in mind: • Only one object can be `self` at a given time • `self` is constantly changing as a program executes • When you call a method, the receiver of that method becomes `self` • All methods without an explicit receiver are called on `self`

👉 Kirill Shevchenko shared a code sample showing how to store cache and session in Redis using redis-rails gem

By default, Rails stores cache into memory in the same process and Session into encrypted cookies, but you can store both in Redis. For cache, Rails has a built-in configuration, and for session configuration, you can use, for example, the "redis-rails" gem

👉 Greg Navis shared a thread about how Active Record queries are cached in controller actions. I invite you to read the entire thread where he explores how this works, and it shows code samples from Rails handling the cache:

2022-11-04 12:00:09 UTC 💡 Rails tip: Active Record queries are automatically cached in all controller actions Result: running the same query again while handling a request will NOT hit the database. ⚠️This does NOT imply "run queries willy-nilly"⚠️ But there's more ...

👉 Matt Swanson shared Ruby English-friendly alternatives to gsub and chomp:

Programmers coming from other ecosystems are often shocked at much Ruby looks like pseudocode. Instead of using methods like `gsub` or `chomp`, you can write super readable alternatives with `delete_prefix` and `delete_suffix`

👉 Josh Cheek shared a code sample about various forms of “class method”:

Example of why I find "class method" a dubious term:

Joel Drapper shared a short explanation:

👉 Greg Navis shared a tip about how to run two branches in the same time with git worktree

Let's rehash the steps: 1️⃣ Create a worktree 2️⃣ Go to the worktree directory 3️⃣ Checkout the branch 4️⃣ Start Rails on a different port

👉 Jim Gay shared a code sample about how to define a method that should be implemented in subclasses:

If you have read so far and you like the content, maybe you take into consideration sharing this and subscribing:

Related (but not Ruby-specific)

👌 Jason Swett shared advice about not making premature generalization:

Programming tip: Don't prematurely generalize. The logic is the same for that of premature optimization: when you try to solve the problems of tomorrow today, your efforts will often turn out to have been a waste.

He also asked about why duplication is more acceptable in test code than in application code:

It's often said that duplication is more acceptable in test code than application code. Why? (I have my own answer but I want to hear what others have to say.)

👌 Chris Mc Cord shared a quote from the creator of VueJS Evan You:

It's pretty wild watching the pendulum swing back to server side rendering: "Not only is it the default, the Next documentation also recommends users to stay in server mode as much as possible to improve end-user performance.” via @youyuxi

👌 Tobias Petry.Sql shared an SQL tip about how to place NULL values at the start or end of a query:L

⚡️ Database Tip Sorting on nullable columns is nerve-stretching: They most probably will not be placed in your desired order. However, you can simply change the placement of NULL values to be at the start/end depending on your requirements.

👌 Joel Drapper shared about the problem with sustaining independent open-source projects:

Here’s a thing that’s sad: the only viable way to fund an independent open-source project is to use it to promote paid closed-source courses and ebooks. You can’t give those things away for free because engineers can’t expense sponsorships on their personal development budgets.

Lucian Ghinda also shared an idea about how to support open source:

An idea for tech companies: Please allow your employees to spend the development budget on sponsoring open-source software. The people doing open-source work or creating things for the dev community are creating knowledge!

👌  Dave Paola shared a good thread about onboarding juniors:

Just had a conversation with a junior developer that highlights so many of the issues many companies face. The issues were numerous but there is one that outshines the others: lack of great onboarding.

If you are interested in this subject, go ahead and read the entire thread and also you might want to check out these two articles from PlanetArgon:

Articles and Videos

Something to read

Newsletters

🗞️ Ruby Weekly published a new edition 627: Sidekiq 7.0 – it's now embeddable

🗞️ Ruby LibHunt published a new edition of the Awesome Ruby Newsletter

✍🏾 Articles

Something to watch 🎥 or listen 🎧

Videos

🎥 Nate Berkopec shared their talk at Kaigi on Rails is now on Youtube: All About Queueing In Rails Applications

🎥 Joel Drapper shared a new pairing session with Kasper Timm Hansen about Mixing HTML attributes in Ruby

🎥 Joel Drapper shared a new episode where he paired with Chris: Coding with Chris - Goblin Grinder part 2 

🎥 Drifting Ruby shared the video walkthrough for This Week in Rails - Nov 4th, 2022

Audio & Podcasts

🎧 Drew Bragg published a new episode with Noel Rappin where they discussed the new Pickaxe book for Ruby 3.2: Episode 11 - Noel Rappin 

🎧 Matt Swanson published the first episode of the new podcast called YAGNI (You Ain't Gonna Need It). The episode is with Chris Toomey, and it is about Detailed commit messages

🎧 The Bike Shed published a new episode 360: ActiveRecord Models

Gems, Libraries, and Updates

🧰 Xavier Noria shared a new release of Zeitwerk 2.6.2. Check the changelog and be mindful not to use any private interface. Xavier is planning to protect the private interfaces:

This release starts a series of gradual patches in which private interface is enforced with stricter formal visibility. (source: https://github.com/fxn/zeitwerk)

🧰 Marco Roth shared that Stimulus released a new version v3.1.1. Read the changelog

🧰 Andrew Culver published a new gem bullet_train-routes. Read the changelog. Here is what it looks like:

Terri O shared a tool to check for vulnerable openssl cve-bin-tool:

For anyone handling "I want a list of every thing we have that uses openssl" today: https://t.co/V11E07iVe9​ cve-bin-tool --runs openssl <directory> To check just for vulnerable openssl, or cve-bin-tool <directory> To see if you have any other "fun" surprises. #infosec

🧰 Nate Hopkins published a new version of TurboReflex v.0.0.13 

🧰 Mike Perham shared that Sidekiq OSS, Pro and Enterprise v.7.0.1 are released. Read the changelog

Reply

or to participate.