This week's collection covers performance optimizations in Ruby’s YJIT 3.4 release to practical tips for improving Turbo forms with custom events. These articles provide developers with valuable knowledge to take their projects to the next level. Additionally, solutions like auto-saving Rails forms and addressing Ruby's mutable object issue demonstrate how the Rails and Ruby communities are constantly evolving to address real-world challenges and improve the developer experience.
YJIT 3.4: Even Faster and More Memory-Efficient
YJIT 3.4, released in January 2025, brings significant performance improvements and better memory efficiency for Ruby, with a 5-7% increase in speed compared to YJIT 3.3.
This version introduces various optimizations, such as enhanced inlining of small methods, better support for profiling and memory allocation, and faster execution of pure-Ruby core methods.
YJIT 3.4 also reduces memory usage compared to previous versions, making it more efficient, particularly on x86-64 systems. Shopify's use of YJIT 3.4 during peak traffic periods, like Black Friday, demonstrated substantial speed improvements in production.
The release also adds a more intuitive
--yjit-mem-size
option and several bug fixes, continuing the YJIT team's commitment to improving Ruby's performance and stability.
Introducing Rails Stats
This article introduces railsstats.com, a new platform designed to showcase the size and scale of Rails projects. By using the built-in
bin/rails stats
command, developers can gather metrics such as lines of code (LOC) and test coverage, which can then be submitted to railsstats.com for sharing with the community.The site aims to collect and compare Rails projects of various sizes, ranging from small projects (under 25,000 LOC) to large enterprise applications (over 500,000 LOC).
The project is open source, built with a simple, vanilla Rails stack using Hotwire and Kamal for deployment. The platform serves as both a tool for sharing project metrics and as a source of inspiration for those considering the scale of their own Rails applications.
Adding button loader to Turbo-powered forms
The author describes a method for improving user experience in Turbo-powered forms by adding a loading spinner to the submit button during server response waits. This is particularly useful in modals or on slow connections where users might otherwise be left without visual feedback.
The solution involves creating a small Stimulus controller that changes the button's text to a loading spinner, disables it, and preserves its dimensions to prevent layout shifts.
The button's width and height are maintained, ensuring the form layout stays consistent. The controller can be easily attached to any form by adding specific data attributes, and for simpler cases, the
data-turbo-submits-with
attribute can be used to avoid the need for a custom Stimulus controller.
Auto-generating frozen_string_literal comments with RuboCop
Tim Riley shares a solution for auto-generating the
# frozen_string_literal: true
comment in Ruby files using RuboCop. By default, RuboCop doesn't automatically add this comment because it considers theStyle/FrozenStringLiteralComment
cop to be unsafe, as it could potentially break code that mutates strings.However, Riley explains how he modified his RuboCop configuration to enable autocorrection for this comment, deeming it safe enough for his personal projects. The key configuration change is setting
SafeAutoCorrect: true
for theStyle/FrozenStringLiteralComment
cop.With this setup, hitting save in his editor triggers RuboCop to add the comment automatically, bringing him small joy with each new file. He ends by expressing hope that one day the comment may no longer be necessary.
Use cases for Turbo’s Custom Events
The article explores the use of Turbo's custom events in Rails, which can enhance user experience and maintainability by triggering actions before, during, and after specific events.
It provides two practical use cases: showing a loading state during frame rendering and resetting a form upon successful submission. For the loading state, Turbo events like
turbo:before-frame-render
andturbo:frame-render
can trigger a spinner, while a Stimulus controller handles the display and removal of the spinner.For form resets, the
turbo:submit-end
event can trigger a form reset if the submission is successful, using a simple Stimulus controller. The article encourages readers to explore Turbo's custom events for cleaner, more efficient code and invites suggestions for additional use cases.
Auto-saving Rails forms with Turbo Streams
The article explains how to implement auto-saving for inline forms in Rails using Turbo Streams and Stimulus. The goal is to automatically save user input, such as a post's title, without needing a traditional "Save" button. It involves creating a small Stimulus controller that submits the form on certain events (like losing focus) by using Turbo's requestSubmit method.
The form remains standard but is wrapped with the necessary Stimulus data attributes. On the server side, the update action responds with a Turbo stream instead of a redirect, allowing for dynamic updates, such as showing a "Saved" message or error feedback.
The process ensures inline auto-saving with user feedback. The article also cautions about potential focus issues when using multiple forms on a page and emphasizes the importance of unique IDs for each input field.
Surprisingly useful Rails console tips
The article provides a comprehensive guide to using the Rails console effectively, highlighting various features and techniques that enhance development efficiency.
It covers practical tips like silencing SQL logs for cleaner output, accessing the last result with the
_
variable, and creating workspaces for focused navigation between object contexts.The guide also explores inspecting object methods and instance variables using commands like
ls
andy
for YAML output, as well as temporarily altering methods without changing the codebase.Additionally, it explains how to run Rake tasks, test routes, and reload code directly from the console, streamlining development workflows.
Advanced tips include finding method definitions, executing raw SQL queries, and inspecting database tables, making the Rails console a powerful tool for developers.
A Community-Driven Solution to Ruby’s Issue With Mutable Objects
At RubyConf 2024, the Ruby community united to address a long-standing issue with Ruby's handling of mutable objects in new hashes.
Nadia Odunayo, in her keynote, highlighted the unintuitive behavior where mutable objects like arrays and hashes, when set as default values, can lead to unintended side effects.
This behavior often confuses developers, especially newcomers. A solution emerged through RuboCop, a Ruby static code analyzer, with a new rule that alerts developers when mutable objects are used as default values in
Hash.new
.This rule ensures each hash key gets a unique or immutable default value, preventing bugs. The community's collaboration resulted in a practical solution, reinforcing the power of open-source teamwork in solving real-world programming challenges.