Short Ruby Newsletter

Share this post

Issue #5 - 8-14 August 2022

newsletter.shortruby.com

Issue #5 - 8-14 August 2022

The one with some optimization ideas

Lucian Ghinda
Aug 15, 2022
3
Share this post

Issue #5 - 8-14 August 2022

newsletter.shortruby.com

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.
Source: @avdi on Twitter

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.
Source: @bbatsov on Twitter

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

Source: @joeldrapper on Twitter

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:

Source: @joeldrapper on Twitter

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
Source: @stevepolitodsgn on Twitter

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
Source: @bhserna on Twitter

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

Ruby devs: what is the most annoying part of your workflow?
Source: @penelope_zone on Twitter

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
Source: @siaw23 on Twitter

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:

Source: @GregMolnar on Twitter

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) ```
Source: @strzibnyj on Twitter

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.
Source: @bhserna on Twitter

Other people suggested some other ways:

  • To use Window Function (suggested by Sunny)

  • To use the Preload API (suggested by Tobias)

  • To use scoped has_one (suggested by Паша Грей):

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
Source: @tpepost on Twitter

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:

Source: @GV1180 on Twitter

Andrea Fomera shared a tip about using ActiveRecord#signed_id:

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
Source: @afomera on Twitter

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

Source: @ekrubyguy on Twitter

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
Source: @software_writer on Twitter

Nick Schwaderer shared an experiment with CGI in Ruby:

Source: @schwad_rb on Twitter

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.
Source: @spickermann on Twitter

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
Source: @piotr_murach on Twitter

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.
Source: @kirill_shevch on Twitter

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.
Source: @andrewculver on Twitter

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.
Source: @tenderlove on Twitter

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.
Source: @rose_w on Twitter
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.
Source: @rose_w on Twitter

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!
Source: @mynameisharry on Twitter

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

Source: @coderbyheart on Twitter

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:

Source: @tobias_petry on Twitter

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
Source: @FriendlyTester on Twitter

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?
Source: @joeldrapper on Twitter

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
Source: @SatntoshYadavDev on Twitter


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

Andrea Fomera launched a new course: Learn Rails by Building Instagram

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

Akshay Khot wrote an article about How to Check if a Variable is Defined in Ruby

timnan wrote an article about CRUD Actions with Turbo Streams and Turbo Frames

Katya Kalache shared an article by Guillaume Montard and Tanguy Joannot about How to secure a Ruby on Rails App.

Greg Molnar shared the new issue of This week in Rails.

r7kamura shared an article they wrote about Release notes management.

Hiroshi SHIBATA shared an article about Understanding /proc

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

Ruby LibHunt shared an article written by Kanani Nirav about Debugging a Ruby On Rails Application in Visual Studio Code

Andrew Mason shared a new issue of Ruby Radar: Ruby Radar #63 - Monitoring Air Quality with Ruby

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

Drifting Ruby shared a video log about This Week in Rails - 2022-08-12. Watch it here - This Week in Rails.

CJ Avilla shared a video made by Mike Rogers. See if here → Resize Images with Active Storage in Rails

Remote Ruby published a new episode. Listen to it here → Rubygem Idea for Juniors, Modern Assets in Rails & George Jetson's Birthday

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:

Source: @maciejmensfeld on Twitter

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

Kerri Miller shared that rails-erd 1.7.2 is out

Outro

Thank you for reading this. If you have any suggestions of people/tweets to include, please reach out to me @lucianghinda or at ideas@ghinda.com.

Share this post

Issue #5 - 8-14 August 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