zaxgood.software
"stuff should be better"

A Quick & Dirty Ruby + Rails Refresher

2024-04-25 09:04

A friend asked me for resources on the latest Ruby/Rails… stuff!—He’s catching up from a late Ruby ~2.x/Rails 5.x standpoint.

This is roughly what I sent him.


General resources

Here’s a non-exhaustive list of stuff I read on the regular. There’s more I’m sure, but this is what jumped to mind immediately.

  • Ruby Weekly — Curated news from around the wider Ruby community. Often just someone’s blog post where they go, “There’s a new Rails thing out, let’s explore it.” They’ve got an RSS feed, too if email isn’t your thing.
  • Ruby on Rails official blog — They drop changelog-ish posts every now and then with write-ups & links to pull requests.
  • Noel Rappin’s blog — Noel’s written a lot of Ruby books, I can personally vouch for Rails Test Prescriptions and his blog post series “Better Know a Ruby Thing.”
  • David Copeland’s blog — Not necessarily Ruby/Rails-specific, but often Ruby/Rails adjacent—I’ve always liked his writing and his viewpoints around “sustainable” processes have shaped my own.

Rails 7ish specific stuff

  • Hotwire — the new default Rails frontend. I think it’s very, very good for a lot of reasons, but also because the developer ergonomics are killer. It’s composed of 3 other technologies:
    • Turbo — Not the Turbolinks of old, but a new hot version with WebSockets. Let’s you do SPA-like shit with server-side rendering through data- attributes and a new respond_to format.
    • Stimulus.js — When you need to get more low-level-JS-fiddly than Turbo, Stimulus.js let’s you write component interactivity organized into controller classes.
    • Strada — You don’t need to know about this unless you want a native app mobile. Strada let’s Turbo Native apps (essentially custom browser wrappers) talk to iOS/Android natively.
  • Hotrails.dev — is a free tutorial site that for Hotwire that’ll get you up to speed quick. I’ve read it like 1.5x, but haven’t taken the time to step-by-step it.
  • Import Maps — are the replacement for Webpacker in Rails 7.x you can read more about Rails-specific stuff on importmap-rails README.
    • The gist is that the HTML includes a manifest JSON object that specifies JS files to load, which are versioned and ETag’d to speed up browser-caching of asset files. Contrasted against packing everything into one huge file, you can see why they went this direction.
    • Node.js and Yarn are no longer requirements. It’s neato.
  • Railsdiff — is a tool for comparing any two versions of Rails. I’ve found it very useful for major/minor version upgrades. It’s pretty much a raw diff, but it’s useful to see config files/initializers that may change between versions.
    • On major version upgrades: upgrading 4.x to 5.x is a slog; 5.x to 6.x to 7.x is pretty breezy—they put in a lot of work to make upgrades easier these days, mature frameworks FTW.
    • Kamal — Essentially Capistrano for Dockerized apps.

Ruby-specific Stuff

He asked specifically about &. syntax, I’ll explain it again outside of a text message.

Essentially it does a .nil? check on the caller and returns nil if true, otherwise it calls the chained method.

So it’s handy for refactoring checks like foo.nil? && foo.do_something into foo&.do_something

# old busted:
cool_object = [CoolThing.new, nil].sample
  # => could be nil, could be a thing, ¯\_(ツ)_/¯

if !cool_object.nil? && cool_object.radical?
  puts "This cool object is radical!"
end

# new hotness:
if cool_object&.radical?
  puts "This cool object is radical!"
end

I’m not super great at keeping the different Ruby versions separated in my head until I try to use something that’s not yet available in the version I’m currently working in—so I don’t have a lot of advice other than to subscribe to the official Ruby blog.


If you’ve got a cool thing I should be reading please drop a line to zach@zaxgood.software.