Edition #14 - 10-16 October 2022

The one with the map of Ruby books

If you want a short excerpt of this newsletter containing only some of the images with code, I created one here. But I invite you to read the entire newsletter as it has excellent content 😊.

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

👉 Krzysztof Witczak shared on Reddit a map of books for learning Ruby:

👉 Thiago Massa shared an interesting idiom to build DSLs in Ruby:

Method chaining in Ruby is an interesting idiom for building DSLs(Domain Specific Languages). I remember when I first saw code().like().this(), my first reaction was "Oh, this is cool!" and felt the need to learn how to do it. 🧐 Here's a short and simple explanation: 👇

👉 Greg Navis shared a code sample showing how to use where.not and where.missing

💡Rails tip: Active Record where chains help you to: ✅Negate WHERE conditions ✅Find models with an empty association ✅Find models with a non-empty associations where.not is familiar to most, but where.associated and where.missing are much less known. More in the next Tweet!

👉 Alexandre Ruban shared about extending a module:

Today I learned something new about Ruby: You can extend a module directly on an instance to access the methods of the module on this specific instance (the C part in the picture 🤯)

Joel Drapper shared an example where he uses this on Phlex and Josh Cheek shared an article explaining how to use extend while implementing decorator pattern.

👉 Kirill Shevchenko shared a code sample about how to use catch/throw in Ruby:

catch/throw vs raise/rescue catch/throw is less commonly used than raise/rescue and is not related to exception handling. It allows you to exit blocks back to a point where a catch is defined for a specific symbol. For example, this can be used to exit nested loops

Adam Rice replied with an example of using this with ActiveInteraction gem:

👉 Greg Navis shared code sample about combining Active Record conditions with OR:

💡Rails tip: `or` can be used to easily combine relations into one. ✅Helps to build complex queries in a step-wise manner ✅Works on GROUP BY + HAVING ✅Can be called multiple times to combine 2+ relations ✅Results in one database query ✅No worries about duplicates

👉 Joel Drapper shared an update about Phlex that will support content for and thus be able to work with existing ERB layout templates:

Currently writing a guide for migrating an existing Rails app to Phlex. One of the recommendations will be to work with the existing ERB layout template unless you’re starting a new project.

👉 Konnor Rogers shared a small piece of knowledge about how Trix works with attachments:

Ever wonder how Trix can load custom attachments one way in your editor and another way in the final output? Well, today I learned how. Theres a sneaky method call "to_trix_html" which will render your attachments based on "to_trix_attachment_partial_path"

He also shared the line of code from Rails that handles this. If you are interested in customizing ActiveText, maybe you want to follow the Konnor series of articles on dev.to about Action Text.

 

👉 Peter Solnica shared about where to place POROs continuing to answer a question that Jacob Daddario asked one week before.

🧵 Kent Beck's "First make the change easy, then make the easy change" applies here. There's no single best way of organizing a codebase. Rails gives you initial structure, which is a good start, but it shouldn't end with the 3 directories in "app/" (1/9)

Here is a summary, but please read the entire thread (nitter link or Thereaderapp ) from Peter as he shares there also the why and more wisdom about how to think about organizing Rails code:

👉 Lewis Youl shared  a code sample about how to access the record that caused an error from ActiveRecord::RecordInvalid error:

ou can access the record that caused an error from the error instance when rescuing from ActiveRecord::RecordInvalid. Useful for when you might have a complex workflow that needs to happen when creating or updating a record. #ruby #rails #rubyonrails

👉 Lucian Ghinda shared a code sample showing the ignore_columns property of Active Record:

How to safely remove a column in Rails It works for Rails 5+ #codesummary #code #ruby #rails #removecolumn

👉 Greg Navis shared advice to use symbol-based error codes instead of string literals:

 💡Rails tip: use symbol-based error codes, instead of error message literals Using literal error messages leads to problems with: ⚠️Translating the app ⚠️Processing error messages via code ⚠️Testing errors in unit tests Using symbols solves all these problems!

👉 Akshay shared a thread explaining howt &:method work in Ruby. Here is one of the tweets in the threadL

👉 Nate Berkopec shared a performance tip for web apps with DB backends:

When you see linearly increasing latency on a web endpoint over time, across all your servers, check for backfill jobs adding lots of rows in tables that previously had very few. Probably exposing a missing index

👉 Kasper Timm Hansen shared they wrote a helper to explore and work with Rails routes in console:

👉 Thiago Massa shared a code sample about using prepend in Ruby:

Ruby's prepend. Did you ever use it? 🧐 This was added in Ruby 2 to improve how you can extend existing code without changing it (AKA monkey-patching 🙈🩹). It's also a very powerful tool to run code before/after the real method is run. 🤯 Take a look 👇

In case you are looking for an explanation about the difference between prepend and include Paweł Dąbrowski replied with the following explanation:

Prepend is like include, it adds the method as the instance method, but while prepend places the module at the beginning of the ancestors' chain, including places it after the class in which it was included.

Pawel wrote a detailed article about this: Extending Ruby Classes

There is also a shorter explanation of what prepend does, replied by Karl Oscar Weber:

So prepend is like, reopening the class and redefining methods, but only you can still call the methods that you redefine, by using Super.

👉 Emmanuel Hayford shared a code sample for seeing the location of a method in Ruby with source.display provided by the method_source gem (included by default in Rails):

2022-10-13 15:08:04 UTC A lot of the times when you use `source_location`, all you want is to see what a method is doing.. so you get a location, navigate to the file to inspect the method. Well, you can save those trips by using `source.display` to have a peek from your terminal.

👉 David Copeland shared a thread with a critique of the static typing people say about dynamic typing:

I RARELY experience type errors in prod with Ruby outside of nil issues, which are prevalent in pretty much every popular compiled language, too. I DO get type errors in tests, but in tests are of functionality, not type-checking, so this argument has never resonated with me

Here is a summary of some of the replies in that thread:

It is also worth reading the tweet from Xavier Noria:

Same, I don't test that user.invoices returns a collection of invoices. My test suite uses the invoice API that won't work if I returned a array of Subscription object. It's very, very rare to check types. And don't get type errors in production.

👉 Kirill Shevchenko shared a code sample about how to run a system command:

There are at least three ways to run a system command from Ruby: 1. backticks (returns a command output) 2. system (returns true/false) 3. exec (doesn't return anything and replaces the current process) Here is a cheatsheet

Kirill also shared three more ways for more complex manipulations:

  • IO.popen

  • Open3

  • PTY

It is also worth considering this reply from Josh Cheek with an example of how to use Open3 in Ruby:

👉 Nate Berkopec shared a tip about how to make the test suite run faster:

Adding things to the "default" setup of a factory, like User/Company/OtherGodObject, should be viewed with as much skepticism as adding a default_scope. Inevitably, they add tons of slowness, and create state only necessary for 1% of tests.

👉 Greg Navis shared about less known Rails settings:

💡Rails tip: Rails has a TON of settings. Below are three settings that may come in handy in most projects: 1️⃣Raising an error when batch processing replaces ordering 2️⃣Warning when fetching too many rows 3️⃣Async destruction via a dedicated queue What should I discuss tomorrow?

👉 Joe Masilotti shared a quote from someone wondering if they should learn Ruby and Rails:

This seems to be a problem in Ruby/Rails world, as there are very few jobs for juniors. Thus, this does not attract so many people. I think you should read all the replies. It is hard to summarise them here.

It is also worth reading this thread from Nate Hopkins quoting Joe and adding some context:

And this reply from Morgan VanDerLeest:

👉 Lucian Ghinda shared a thread about alternative ways to freeze strings in Ruby, showing the String#-@ method:

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)

🧐 Óscar M. Lage Guitián shared a tip about formatting JSON with jq in Vim:

This afternoon I had a meeting, my mate prettified a horrible string into a tabulated and perfectly aligned json in his shinny VSCode. Literally 4 mins later in my .vimrc: :command Format :%!jq . :command Unformat :%!jq -c . #vim #neovim

🧐 Brandon Weaver shared a nice quote about calm and living life:

Just a reminder for folks: You don't have to code on the weekends or every day after work to be good at your job. You're allowed to just exist.

🧐 Ryan asked why people use git rebase:

Confession time: I never use `git rebase`. Do you?

Here is a summary of the replies comparing `git rebase main` with `git merge main`:

🧐 Brandon Weaver shared a thread (nitter link) about engineering leadership:

We have such a culture of grinding, alpha/sigma/whatever people are on about now, and becoming some mythical 10x engineer when in reality the answer is so simple and has always been right in front of folks: The entire point of leadership is others.

🧐 Danielle shared a short tip about getting your public key from Github:

github[.]com/[username].keys continues to be the most useful thing for setting up new computer hardware 😅

Here are two more tips shared in the replies to this tweet:

🧐 Dare Obasanjo shared a take on the source of large-scale product problems:

Most large scale product problems are either organizational or prioritization problems. The challenge is that techies tend to think they’re technical problems when in truth it’s a failure to communicate or to prioritize. This is one of the ways experience is an advantage.

Articles and Videos

Courses

Something to read

🗞 Newsletters

✍🏾 Articles

Eric Berry shared a very detailed article about The Rails SaaS Conference

Andrea Fomera shared two articles about implementing mentions with Action Text. Read Combined Mentions with ActionText (Part 1) and Combined Mentions with ActionText (Part 2)

Vladimir Dementyev shared a new article they wrote about using View Components: Building modern Rails frontends

Something to watch 🎥 or listen 🎧

🎥 Videos

🎥 Adrian Marin shared the presentation done by Yaroslav Shmarov at @wrocloverb about 18 months of using hotwire and viewcomponent in production. Yaroslav shared the slides used in that presentation here

🎧 Audio & Podcasts

🎧 Ruby For All shared a new episode about imposter syndrome. Listen Episode 12: Imposter Syndrome is Among Us

🎧 Joel Quenneville shared a new episode of The Bike Shed with Amanda Beiner. Listen 357: Notetaking For Developers

🧰 Gems, Libraries, and Updates

🧰 Avo shared a new release with a resource sidebar, native field components, custom polities and a lot of bug fixes. Read the changelog or watch the video.

🧰 Josef Strzibny shared a tool for encrypting JSON, YAML values, and not key: See mozilla/sops

🧰 Vinicius Stock shared an update for the Ruby LSP server gem and for the VSCode plugin. Read the changelog for Ruby LSP Server and the one for the VSCode Plugin. It is also worth taking a look at the documentation.

🧰 Nate Berkopec shared announced the release of Puma 6.0 named Sunflower. Read the changelog

🧰 PostgreSQL announced the release of PostgreSQL v15

🧰 Yuri Sidorov shared a new gem called jbuilder-schema that will:

It automatically generates JSON-Schema from #jbuilder templates, which is useful to auto-generate OpenAPI documentation of your existing APIs.

🧰 Chris Oliver shared the Github ci.yml they use for Noticed that will run Github Actions on PG, MySQL, and SQLite.

🧰 Nejdet Kadir shared an extension for Phlex to use heroicons as SVG: phlex-heroicons:

🧰 Xavier Noria shared they are working on adding constant_path_at, namespace_at and eager load a directory to Zeitwerk

🧰 Peter Solnica announced they added Zeitwerk to almost all dry-rb gems inviting people to test and report if there are any issues.

🧰 Joel Drapper sharing a discussion where he explains how to create a Phlex view that behaves similarly to how ViewComponent works with slots.

🎵 Music for coding

A random selection of coding music:

  • Coding Playlist | (almost) No Vocals - Spotify - electronic music

  • Inspired Moment for Coding - Spotify - modern jazz

Reply

or to participate.