Issue #5 - 8-14 August 2022

The one with some optimization ideas

Hello,

Thank you for subscribing to this newsletter. It might be too long for some email clients, in this case, please read it online here. No worries, even if it is long, it is easy to read 😀 as it contains tweets.

If you like it and have suggestions of people working with Ruby and sharing exciting stuff on social media, please find me on Twitter @lucianghinda and tell me about them.

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.

Avdi Grimm shared why Ruby feels simple:

Ruby is complex syntactically but simple semantically. There are so many examples of this. E.g. it has the easiest-to-remember rules for method argument default values I've ever encountered: any expression that would be valid body in the method is a valid default. End of story.

Bozhidar Batsov shared his vision about how Rubocop embraces regional dialects of Ruby:

Compared to the approach taken by Standard, RuboCop really tries to embrace the regional dialects of Ruby. I have to admit that 10 years ago I favoured the Standard approach (there's one Way to do things), but now I'm way more appreciative of Ruby's diversity of expression.

Joel Drapper shared more about the progress to make Phlex.fun faster

If you want to learn more about performance improvements done so far, Joel invited us to browse the pull requests tagged with performance.

Here is the latest performance benchmark:

Steve Polito shared an example of using aggregate functions in PostgreSQL:

result = ActiveRecord::Base.connection.exec_query( <<-SQL SELECT AVG(total_sign_ups) AS sign_ups_per_day_on_average, STDDEV_SAMP(total_sign_ups) AS standard_deviation FROM( SELECT COUNT(*) AS total_sign_ups FROM users GROUP BY created_at::date ) AS users LIMIT 1 SQL ).to_a # => [{"sign_ups_per_day_on_average"=>1.2, "standard_deviation"=>0.54387344054267909474}] sign_ups_today=User.where("created_at::date = ?", Time.now).count # => 1 # ℹ️ This can be run in a daily cron job if sign_ups_today < result.first["standard_deviation"] raise Anomaly::UserSignUpAnomaly end

Benito Serna explained when chaining works or does not work with ActiveRecord:

# You can chaing query methods without executing the query, like if you where # building an sql query posts=Post.order(: published_at).where (published_at: ..DateTime.current) .joins(:comments) .preload(:comments, :author) # It will execute the querv until you asks for the actual records with methods like # to a, find or last. relation=ActiveRecord::Relation comments=Post.find(id).comments puts comments.is_a?(relation) #=> true puts comments.order(:id).is_a?(relation) #=> true outs comments order(:id).last.is_a?(relation) #=> false

Penelope Phippen asked a good question about the annoying part of Ruby devs:

Ruby devs: what is the most annoying part of your workflow?

Here are some examples of annoyances from that thread

  • Setting up a new machine

  • Choosing a debugger

  • Documentation UX

  • “unjustified” Rubocop rules breaking CI

  • Flaky Capybara/Selenium/Headless-chrome tests

  • Using Docker on Mac

  • Installing native extensions

Emmanuel Hayford shared a TIL about threads spawned by MRI/YARV

TIL that when MRI/YARV starts, it spawns 2 threads: One for the main thread, the other registers handlers for Unix signals. Signals are the basic way for processes to communicate with each other on a Unix system. Used to think only the main thread run

Greg Molnar shared an excellent thread about using the single-file Rails application. Read the all thread starting from here. The final file might look like this:

If you want to try this with Ruby 3, you need to add webrick gem to the list of gems (source).

Josef Strzibny shared a Docker snippet about how to configure Bundler:

One universal snipped for your Dockerfile's bundle: Ruby applications will install their dependencies using Bundler and it's a good idea to set your own BUNDLE APP CONFIG', "BUNDLE PATH' and "GEM HOME' variables ```bash RUN gem install bundler--no-document COPY Gemfile Gemfile.lock / app/ ARG BUNDLE APP CONFIG="/app/. bundle" ARG BUNDLE PATH=/app/vendor/bundle RUN bundle config set --local path $BUNDLE PATH &&\ bundle config set --local without development: test &&\ bundle config set -- local frozen 'true' RUN bundle install --no-cache --jobs $ (nproc) ```

Benito Serna shared two simple solutions to N+1 queries when the child model needs to be ordered in a specific way. Here is one example from the thread:

2. Solve the problem with an association with default order • Provide an scope to the association • Call comments.last because the comments are already ordered It won't execute n+1 queries, because you are fetching the comments already on the right order.

Other people suggested some other ways:

class Post has many :comments has_one : last_comment, -> { order (created_at: :desc) }, class_name: "Comment" end Post.all.includes(:last_comment) .each do |post| puts post.last_comment end

Benito posted other tweets about preloading associations: preloading belongs_to and has_many associations, preloading nested associations.

Gustavo Valenzuela shared they are creating a list of open-source #rails projects:

Making a signed id with a purpose and expiration is the feature from Rails more people should know about. Soooo useful for so many things

EK shared a new board created on RubyOnRemote.com about Ruby developer’s salaries:

Akshay Khot shared a quick spec about how defined? keyword works in Ruby. The article goes a bit deeper into examples:

RSpec.describe 'Defined' do it 'tests if the local variable is defined' do name='Akshay' expect(defined? name).to eq('local-variable') expect(defined? b).to eq(nil) expect(defined? nil).to eq('nil') expect(defined? String).to eq('constant') expect(defined? 1).to eq('expression') end it 'ensures that a variable set to nil is still recognized' do name=nil expect(defined? name).to eq('local-variable') end end

Nick Schwaderer shared an experiment with CGI in Ruby:

Martin Spickermann reshared an older tweet showing performance for a Rails API:

How fast is a #rubyonrails app in API mode with well-chosen indexes and 35M+ rows in a #mysql database? Well, the average response time is, according to #newrelic, below 6ms.

Piotr Murach shared some excellent Ruby CLI gems created by Andrew Kane:

For those who work with #PostgreSQL 🐘, @andrewkane has created a few excellent #Ruby CLI gems 🔥: 💎 pgdexter - autogenerates indexes 💎 pgslice - partitions tables 💎 pgsync - syncs data You can check them all out in the Database category https://github.com/piotrmurach/awesome-ruby-cli-apps#database

Kirill Shevchenko shared a short Ruby code example about calling a deleted method:

Calling removed method in #ruby: Even after a method has been deleted from the class, you can still call it if you keep a reference to it (here is "um" is UnboundMethod) or replace by another method with the same name.

Andrew Culver shared how easy it is to install Ruby on Rails on a new M2 Macbook Air:

Unpacked my new M2 MacBook Air this morning and used rails.new to automatically install Homebrew, Xcode CLI, Git, rbenv, nvm, Node.js, Yarn, Redis, PostgreSQL, Ruby, and Rails. A thing of beauty, really.

Related (but not Ruby specific)

Aaron Patterson voiced a great idea to think about software:

Some software is just done, and I think that's fine.

Rose W wrote an excellent thread about how to start a new project. You should read it all but here are just two quotes:

All this is why I sometimes joke that a Staff Engineering should be called "Asker of questions". It's more important to know what questions to ask than to know all the answers yourself. Knowing what to build is about knowing what to ask.
So now you have a why and what and visuals. Next up, think about how you can be lazy. Less work is better for everyone. Are there open source things you can pull from or help build. What/who at your company has done similar stuff? Look for what you can skip doing yourself.

Harry Llewelyn shared a small tip about using two submit buttons on a form:

TIL: If you have a form that you have two submit buttons on, both going to different places - you can use the `formaction` attribute on the `submit` element or button to override where it submits. No need for Javascript!

Markus Tacker asked an interesting question about the time spent on job applications:

There are a lot of interesting replies to this poll worth reading.

Tobias Petry shared a new database tip about storing tags in JSON array:

Richard Bradshaw asked a question about changing naming the role of a tester and received a lot of thoughtful replies:

"I'm trying to make a case to change the name of our QA Engineers to Test Engineers. do you have any information or articles that explain their reasoning for using "Test" instead of "QA"?" Can I delegate this request to you Testing Twitter? I'm struggling with workload

Joel Drapper asked for resources about writing excellent technical documentation:

Are there any good books or articles I should read on writing great technical documentation?

Among the recommendations:

  • Write Useful Books

  • The Developer’s Guide to Content Creation

  • justsimply.dev

  • diataxis.fr

  • On Writing Well: The Classic Guide to Writing Nonfiction

  • Docs for Developers

Santosh Yadav shared a quick Git Tip that will push new branches to default remote and will set upstream tracking:

git config --global push.autoSetupRemote true

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

Articles and Videos

Courses

Cezar Halmagean shared his Rails course that now includes a Hotwire section: Become a Ruby on Rails Developer

Something to read

Tim Riley wrote an OSS update for May-July where he shares the vision for Hanami 2.0 application structure and much more.

Matthew Gaudet shared an article he wrote about improving Ruby speed by using OMR: Faster Ruby: A Ruby+ OMR Retrospective. The replies to the tweet sharing this article are also worth reading.

RUBYLAND shared an article written by Benito Serna about a gem he created that allows creating a running active record example: Active Record Playground Runner Introduction

r7kamura wrote an article about BUNDLE_ONLY - an option that will allow installing gems only from a specific group(s): BUNDLE_ONLY is now available

r7kamura shared an article they wrote about Release notes management.

Rails Links shared a link about a new open source Ruby project: Multi Blogging Platform

Something to watch or listen

GoRails published a new episode about writing Ruby on a Raspberri Pi to collect Air Quality. See it here → Air Quality Monitoring with Ruby and Raspberry PI

therubyonrailspodcast published a new episode of The Ruby on Rails Podcast. Listen to it here → Dearly Departed (Brittany + Jemma)

Dean DeHart shared a new video they uploaded about extending Rails 7 Trix Action Text with an emoji pop-up picker: See it here

Ruby for all published a new episode about best resources for juniors: Listen to it here → Best Resources for Juniors in 2022

New libraries and updates

Samuel Williams announced that Rack 3.0.0.beta1 was released. See the release here tagged with 3.0.0.beta1, and it is worth reading the Changelog and Upgrade Guide

Bozhidar Batsov announced the release of Rubocop 1.34, containing a lot of bug fixes and small improvements and then the release of Rubocop 1.35

Maciej Mensfeld announced a release of Karafkja 2.0 having a lot of nice new features:

Stan Lo announced that debug.rb released a new version 1.6.2 that contains important bug fixes. See changelog here.

Jeremy Evans shared the release of Roda 3.59.0. See the changelog here

Jean Boussier shared he started to work on redis-rb 5.0

Outro

Thank you for reading this. If you have any suggestions of people/tweets to include, please reach out to me @lucianghinda or at [email protected].

Reply

or to participate.