Short Ruby Newsletter

Share this post

πŸ‘‹ Issue #12 - 26 Sep - 02 Oct 2022

newsletter.shortruby.com

πŸ‘‹ Issue #12 - 26 Sep - 02 Oct 2022

Lucian Ghinda
Oct 3, 2022
8
Share this post

πŸ‘‹ Issue #12 - 26 Sep - 02 Oct 2022

newsletter.shortruby.com

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.


I feel the same πŸ‘‡

Source: @collin_jilbert onΒ Twitter

πŸ‘‰ Thiago MassaΒ sharedΒ how to assign the previous result in Rails console:

Here is a Rails console / irb / debugging tip that well-seasoned Ruby developers know for sure. But if you are just starting out 🐣, you might have missed learning it: A real time-saver, huh? πŸ‘‡βœ¨
Source: @th1agofm onΒ Twitter

πŸ‘‰ Greg NavisΒ sharedΒ code sample for using ActiveModel to validate forms without persistence

πŸ’‘ Rails tip πŸ’‘ Active Model can be used to implement form objects, i.e. objects validating and performing actions that don't fit well with a model. They work great for: βœ… Complex operation-specific validations βœ… Calling third-party APIs βœ… Actions that don't fit a model
Source: @gregnavis onΒ Twitter

πŸ‘‰ Kirill ShevchenkoΒ sharedΒ code sample about using Ruby defined?:

Ruby's defined? keyword is not a method, it's a language feature. It means using defined? doesn't evaluate the assignment, just parsing the argument. So, you can use it safely to check whether a variable/constant/method exists, or check if something is a method or variable. #ruby
Source: @kirill_shevch onΒ Twitter

and here is a reply from Josh Cheek:

Source: @josh_cheek on Twitter

πŸ‘‰ Matt SwansonΒ sharedΒ code sample about using compact_blank method:

You probably want `compact_blank` most places you are using `compact`
Source: @_swanson onΒ Twitter

Lucian Ghinda added to the conversation some ideas in case performance is important:

Source: @lucianghinda on Twitter

Β 

πŸ‘‰ Peter SolnicaΒ sharedΒ a sample code showing how to use rightward assignment while verifying a service result:

Source: @solnic29a onΒ Twitter

πŸ‘‰ Steve PolitoΒ sharedΒ a migration example about create database index based on type cast value of a column:

Did you know, you can create a database index in PostgreSQL based on the **type cast** value of a column? This can be useful if you need to limit the number of records a user can create per day. Here’s an example in #rails.
Source: @stevepolitodsgn onΒ Twitter

πŸ‘‰ Thiago MassaΒ sharedΒ a configuration option to enable strict loading model that will raise error on N+1:

Did you ever have problems with N+1s in Ruby on Rails?🀨 Since Rails 6, there's a strict_loading mode that can help you to never have such issues again (at a cost of not having associations loaded whenever it is needed). Do you already use that feature? Check it out
Source: @th1agofm onΒ Twitter

πŸ‘‰ Jason SwettΒ sharedΒ a nice opinion about what people call Rails magic is not exactly magic and we should think about it more like well documented rules:

Regarding "Rails magic"... Something is magic if it defies the laws of nature or scientific explanation. Rails gives developers powerful leverage. The results are magical but they're NOT achieved via magic. They're achieved through understandable, well-documented _rules_.
Source: @JasonSwett onΒ Twitter

πŸ‘‰ Greg NavisΒ sharedΒ about conditional class names in Rails ERB:

Rails tip: Rendering class names conditionally in ERB is frustrating πŸ˜– Fortunately, there's a better way: πŸ‘‰πŸ» class_names aka token_list Focus on the underlying logic that determines when a class should be applied instead of gluing strings.
Source: @gregnavis onΒ Twitter

πŸ‘‰ Brad GesslerΒ sharedΒ a code sample about running ActiveJob on Fly.io:

Building the basic β€œfire and forget” ActiveJob adapter for Fly Machines was fun!  The repo at github.com/fly-apps/rails… now runs ActiveJobs on Fly Machines. That means its time to write about this at fly.io/ruby-dispatch/.   Subscribe to the feed if you don’t want to miss it!
Source: @bradgessler onΒ Twitter

πŸ‘‰ Brad GesslerΒ sharedΒ a code sample about making markdown-rails renderer work well, including code highlighting and Youtube tags

I finally have a decent Markdown renderer for Rails working. You’ll mostly work from the `./app/markdown/application_markdown.rb` file to customize stuff in Markdown like code highlighting, YouTube tags, etc. What’s great is you can reload the page to see changes!
Source: @bradgessler onΒ Twitter

You should read the entire thread and see it working on his view-playground Rail app on Github. Also, if you want to use it in your rails app, Brad shared how.

πŸ‘‰ DorianΒ sharedΒ how to use ActionController::Parameters with_defaults, which seems to be an alias for reverse_merge:

Source: @dorianmariefr onΒ Twitter

πŸ‘‰ Joel DrapperΒ askedΒ about how Zeitwerk works when handling conflicting root directories:

@fxn how does Zeitwerk handle conflicting root directories? For example if I autoload `app` in Rails, could that conflict with `app/models`, `app/controllers`, etc?
Source: @joeldrapper onΒ Twitter

Xavier Noria answered

Source: @fxn on Twitter

You can read more about how nested root directories are loaded in Zeitwerk docs here.

He then added a suggestion about how to namespace files in a directory:

Source: @fxn on Twitter

πŸ‘‰ Thiago MassaΒ sharedΒ how to run a small piece on code in your Rails app context (like you will run it in the Rails console):

Frequently when using Rails console, we already know what we want to look for, or what we want to run. πŸ’‘ Rails has an often unused, but powerful command-line feature: rails runner. Do you ever use it? 🀨 Let's see how we can use it to our advantage.
Source: @th1agofm onΒ Twitter

πŸ‘‰ AkshayΒ sharedΒ a code sample about how to use Configurable concern:

Rails provides a `Configurable` concern that lets you add configuration options on classes. It uses `OrderedOptions` behind the scenes, so you can use the method-like syntax.
Source: @ak_rails onΒ Twitter

πŸ‘‰ Kirill ShevchenkoΒ sharedΒ how to use Ruby built-in Tempfile class:

Ruby has a built-in Tempfile class. This allows you to create uniquely named tmp files, and instances behave like a regular File objects.
Source: @kirill_shevch onΒ Twitter

Please note that Mika Henriksson warned:

Can have weird β€œdisappearing” side effects inside a docker container though.

Alpine Linux issue perhaps with the temp directory but it caused me a lot of headache. Problem went away after using regular files and cleaning them up after usage.

πŸ‘‰ Robbyrussell.RbΒ sharedΒ how to open a gem in an editor and look at the source code:

I believe gem open to be one of the least utilized commands to run by RoR developers. No need to browse the source code online. You can load it up in your favorite editor like so.
Source: @robbyrussell onΒ Twitter

Here is a summary from all replies and with one thing that I added there about how to explore local installed gems source code:

πŸ‘‰ Prabin PoudelΒ sharedΒ a Rails anti-pattern thread talking about violation of MVC pattern by adding finders in views and controllers:

Source: @coolprobn onΒ Twitter

πŸ‘‰ Benoit BenoitΒ sharedΒ a new feature, just merged to Rails master (probably will be part of Rails 7.1) about conditionally setting log level:

TIL `ActionController::Base.log_at`
Source: @Benoit_Tgt onΒ Twitter

πŸ‘‰ Thiago MassaΒ sharedΒ a code sample about strings and freeze and symbols:

Source: @th1agofm onΒ Twitter

It is also worth considering this reply from Josh Cheek, adding more context:

Source: @josh_cheek on Twitter

πŸ‘‰ Kirill ShevchenkoΒ sharedΒ how to enable YJIT when using Ruby 3.1:

If you're thinking about trying YJIT, you can enable it with the RUBYOPT='--yjit' for a specific process or globally. Requires Ruby version 3.1+.
Source: @kirill_shevch onΒ Twitter

πŸ‘‰ David CopelandΒ askedΒ a question about how to group db updates and enqueue job in a fault-tolerant way:

Source: @davetron5000 onΒ Twitter

There are a couple of solutions proposed there:

  1. Using interactor rollback

  1. Using the newly published changeset gem (you can see an example of code behind this idea of changeset here)

  2. Using Acidic Job principles and ideas - see an example here - but I recommend you to read this thread where David asks questions about the code shared by Stephen to understand more why that solution is written like that.

πŸ‘‰ Thiago MassaΒ shared a code sampleΒ about numbered params in Ruby:

Source: @th1agofm onΒ Twitter

Lucian added this context about how numbered params work:

Source: @lucianghinda on Twitter

It is also worth reading this exchange started with Joel reply about wanting an alias for _1.

If you are interested to read more about numbered params:

  • Read this ticket on Ruby where people discuss it

  • And then this one about re-reconsidering it

I think it is worth having an alias for _1, and it seems like `it` could be great for that.

πŸ‘‰ Greg NavisΒ sharedΒ a code sample showing how to use queue_as with a block to route jobs to different queues:

πŸ’‘ Active Job tip: queue_as accepts a block that can be used to route jobs to different queues dynamically. Example: paid users have priority over free users for expensive jobs. A sensible business requirement! πŸ‘‰πŸ» The next tweet explains how to make this mechanism generic.
Source: @gregnavis onΒ Twitter

πŸ‘‰ Cezar HalmageanΒ sharedΒ a thread about how to migrate from import maps to esbuild:

Migrate from import maps to esbuild with these 5 fixes. If you're trying to migrate from import maps to Bootstrap + esbuild you'll get some errors at build time. Here's how to fix them

πŸ‘‰ Fullstack RubyΒ shared about ::Data being merged to Ruby master:

Struct no more? ::Data has arrived in Ruby. A more modern take on "value objects". Person = Data.define(:first_name, :last_name) me = Person.new(first_name: "Jared", last_name: "White") Works with pattern matching! Details & docs here:
Source: @fullstack_ruby onΒ Twitter

Read the PR on Github for more details about this. Here is a short snapshot of the description provided on the PR by Victor Shepelev:

Source: https://github.com/ruby/ruby/pull/6353

πŸ‘‰ Greg NavisΒ sharedΒ a code sample about filtering sensitive attributes in Active Record:

 Rails tip: Active Record makes it trivial to filter out sensitive attributes. Just add them to a class attribute named filter_attributes on the relevant model. See the example below πŸ‘‡πŸ»
Source: @gregnavis onΒ Twitter

As John Duff noted, this will only filter sensitive information while inspecting. Add params filtering, not to write sensitive information in logs.

πŸ‘‰ Colleen SchnettlerΒ askedΒ what the most important Rails library/gem/package is:

Rails devs - what's your "I can't live without it" Rails library/gem/package?
Source: @leenyburger onΒ Twitter

Here are some of the responses:

annotate, awesome_print, better_errors, bullet, bundle-audit, byebug, capybara, chusaku, devise, draper, dry-monads, dry-validation, faker, faraday, flipper, guard, haml-rails, http.rb, js-routes, kaminari, lockbox, pagy, pay, prosopite, pry-rails, pry-usable, pundit, railseventstore, retest, rspec-rails, rubocop, shoulda, sidekiq, skunk, sorbet-runtime, strong_migrations


πŸ‘‰ Kirill ShevchenkoΒ sharedΒ a code sample about calling lambda and proc with square brackets in Ruby:

Source: @kirill_shevch onΒ Twitter

Josh Cheek added 7 ways to call a proc in Ruby (read more about it here):

Source: @josh_cheek on Twitter

πŸ‘‰ Nick SchwadererΒ askedΒ about problems with Rails:

Please retweet for visibility to your Rails friends πŸ™ I'm on the Scalable Rails Apps team as part of the Ruby and Rails Infrastructure team at Shopify. A lot of our work touches on open source. What is your biggest pain point or missing feature in Rails? #ruby #rails
Source: @schwad_rb onΒ Twitter

Here are some of the answers:


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


Related (but not Ruby specific)

🧐 Richard Schneeman shared a good thread about how to contribute to open source

Over the past 11 years, I have 2.8k GitHub stars on a repo, 1.9 billion library downloads, and over 1k GitHub 🟩 a year. What most people don’t realize is I use a simple 4-step repeatable framework to help me keep going. πŸ§΅πŸ‘‡
Source: @schneems onΒ Twitter

🧐 Brandon Weaver shared an idea about a healthy mindset fore growing and learning:

My greatest asset as an engineer is being able to admit my foolishness and ignorance, rather than assuming my own intelligence.
Source: @keystonelemur onΒ Twitter

🧐 Yasuhiro Matsumoto shared a nice perspective about some of the debates regarding programming languages

Programming languages tend to be judged good or bad by their syntax alone, tend to be dominated only by local benchmarks, and in most cases evaluated by people who haven't actually used them.
Source: @mattn_jp onΒ Twitter

🧐 Heroku announced their new low-cost plans:

Source: @heroku on Twitter

Read the official announcement here and see some reactions here on Reddit /r/ruby

There seems to be another announcement about a partnership with Github to offer Student Developer Program/

If you are an educator not from University you should give feedback on this Github issue and say what you need or how you see the support for this community.

🧐 Dave Paola asked what we do to familiarize ourselves with a new codebase:

What is your #1 tip for familiarizing yourself with a new codebase?
Source: @dpaola2 onΒ Twitter

Here are some of the answers to this question:

🧐 Shopify Engineering asked about what advice one would give to interns or CS students:

If you could give one piece of advice to current interns or CS students, what would it be?
Source: @ShopifyEng onΒ Twitter

Here are some answers to this question, and I invite you to read the entire conversation as it has a lot more ideas:

🧐 Jason Swett shared hot take about frameworks:

Here's something about frameworks that it took me a long time to realize. A framework can give you an initial productivity boost, but once your codebase gets sufficiently large, your framework can no longer help you keep your code organized, and it's all up to you.
Source: @JasonSwett onΒ Twitter

He also added this in one of the replies:

Keeping large amounts of code understandable requires skill, and tools can't be a substitute for skill. (That's why frameworks can only help in the early stages, because they're tools.) - source @JasonSweet on Twitter

🧐 Adrian Marin shared a thread about a category of persona present on social media - the toxic developer.

Source: @adrianthedev onΒ Twitter

It is also worth considering this response from Mika Henriksson:

Source: @mhenrixon on Twitter

Articles and Videos

Courses or Books or Communities

πŸ“š Richard SchneemanΒ launchedΒ their new book How to Open Source. Buy the book at howtoopensource.dev

πŸŽ₯ Noah GibbsΒ sharedΒ the video playlist of Ruby Learning Center. See it here

Something to read

Newsletters

πŸ—ž Emmanuel HayfordΒ shared a new edition of This Week in Rails.

πŸ—ž Marc Busqué sharedΒ their OSS update for September 2022.

πŸ—ž Joe Masilotti shared the September edition of The Hotwire dev newsletter

πŸ—ž Ruby LibHunt published their 332 edition of Awesome Ruby Newsletter

πŸ—ž Ruby Weekly published their 622 edition Sidekiq 7 (beta) and Sinatra 3 (for real)

πŸ—ž Ruby Radar published their 70 edition Hackathon Success

Articles

✍🏾 Austin Gil shared a nice cool hidden feature of VS Code: Timeline view

✍🏾 Robby Russell shared an older article with very good information about how to take over an existing Rails app

✍🏾 Sam Ruby shared an article about how to run Ruby on Rails via region replicated sqlite 3 (what Fly calls LiteFS - distributed file system built for replication SQLite databases)

✍🏾 Steve Polito shared an article about detecting anomalies in user behavior using PostgreSQL

Something to watch πŸŽ₯ or listen 🎧

Videos

πŸŽ₯ Drifting RubyΒ sharedΒ a new episode Downloading Cloud Database. See it on driftingruby.com

πŸŽ₯ Eric BerryΒ sharedΒ an old (but still relevant) video about Arel by Cameron Dutro. See it Advanced aRel: When ActiveRecord Just Isn't Enough

πŸŽ₯ Go RailsΒ sharedΒ a new video created by Collin Jilbert. See it β†’ How to Deploy Rails to Render

πŸŽ₯ Justin SearlsΒ shared a video they created to show a new way to sort Ruby objects. See β†’ A better way to Sort Ruby Objects

πŸŽ₯ Janko MarohniΔ‡Β shared a video they streamed about adding admin accounts to the Rodauth demo Rails app. See Adding admin accounts with Rodauth

πŸŽ₯ Drifting RubyΒ sharedΒ the video log for This Week in Rails. See This Week in Rails Sept 30th, 2022

πŸŽ₯ Aaron PattersonΒ sharedΒ a live stream with Vinicius Stock where they play around with Language Servers. See Pairin' with Aaron: Messing with Language Servers

πŸŽ₯ Adrian Marin recommended we watch a video by RafaΕ‚ Rothenberger from wroclove.rb conference. See Β Devise pitfalls and way to tighten security

πŸŽ₯ Joel Drapper shared a pairing session with Stephen Margheim working on the Phlex compiler. See Phlex Ruby Compiler

Audio & Podcasts

🎧 The Ruby on Rails PodcastΒ sharedΒ a new Ruby on Rails podcast episode. Listen β†’ It's Not Only Sauerkraut and Pickles (Brittany + Nick)

🎧 SchwadΒ sharedΒ a Twitter space recording named β€œCode with Jason LIVE!! It’s not a goodbye forever”. Listen here

🎧 Ruby For All shared a new episode they published where they talk about interviews with Brandon Weaver. Listen to Understanding Interviews with Brandon Weaver

🎧 Remote Ruby shared a new episode about Rails Hackathon and Turbo 7.2 release. Listen to Rails Hackathon 2022 & Turbo 7.2 release

Gems, Libraries, and Updates

🧰 Avo shared a new release of their powerful and flexible Rails admin. Check the release notes here or see a video of the new features here.

🧰 LÑzaro Nixon shared a Github repo of Trix extensions. See them here: github.com/lazaronixon. Examples of what you can find there: multiple headings and choosing between multiple colors.

🧰 Mike Perham shared a Sidekiq 7.0 release. See the changelog. It comes with very nice things.

🧰 Eric Berry shared a new gem used for debugging whoop. Here are some examples:

Source: https://github.com/coderberry/whoop

Among the most exciting parts is the ability to add :explain for logging SQLs.

🧰 Kasper Timm Hansen launched a new gem called active_job-performs that adds the performs macro to setup job by convention. Here is an example:

Source: https://github.com/kaspth/active_job-performs

🧰 Julian Rubisch shared their gem attractor for generating a report with code complexity metrics for Ruby and Javascript. This is a cool gem for knowing how your code base is.

Justin SearlsΒ shared a video that contained a new gem called put that helps with sorting arrays. Here is how the code looks like:

Source: https://github.com/testdouble/put

🧰 Joel Drapper announced a change in Phlex from Phlex::Components to Phlex::Views:

Source: @joeldrapper on Twitter

🎸 Music to listen to while coding

This is just something extra this week, as I discovered on Reddit r/ruby two excellent playlists to listen to while coding:

  • Coding Synth 4 (Spotify)

  • Coding (Spotify)


Please consider sharing this on social media or with your colleagues:

Share

Share this post

πŸ‘‹ Issue #12 - 26 Sep - 02 Oct 2022

newsletter.shortruby.com
Previous
Next
Comments
TopNewCommunity

No posts

Ready for more?

Β© 2023 Lucian Ghinda
Privacy βˆ™ Terms βˆ™ Collection notice
Start WritingGet the app
SubstackΒ is the home for great writing