#​566 — August 19, 2021

Read on the Web

Ruby Weekly

On Modern Rails + Web Apps Without JavaScript Bundling or Transpiling — DHH has explained what approaches the next iteration of JavaScript in Rails will take and import maps have a huge role to play. The goal is to, eventually, not need a bundler like Webpacker and just pull in the ES6 modules you need.

David Heinemeier Hansson (DHH)

“Unless new evidence comes to bear that refutes the basic tenets of this analysis, Rails 7.0 will aim to give you a default setup based on import maps, and leave the Webpacker approach as an optional alternative.”

___
David Heinemeier Hansson

▶  An 'Alpha Preview' of Modern JavaScript in Rails 7 Without Webpack — As tends to be David’s way (from way back when the build a blog system in 15 minutes video boosted Rails’ initial popularity) he’s recorded a practical screencast showing off his latest thoughts.

David Heinemeier Hansson (DHH)

Uptime Monitoring Is Now Available in AppSignal — Uptime monitoring is the first line of defense against downtime. Ping your apps every minute from 4 locations around the world and receive alerts when something is down. Now you'll know about downtime before your users do.

AppSignal sponsor

TTY::Option: A Declarative Command-Line Arguments Parser — An extensive parser that covers just about every CLI scenario one could imagine.

Piotr Murach

▶  How to Debug a Rails App — A nice 30-minute screencast gently introducing you to the practicalities of debugging Rails apps from the basics through to using external tools and gems to help.

Phil Smy

QUICK BITS:

Jobs

Backend Engineer | Remote Within CET (-2/+2 Hours) | Full-Time — Join us as a Backend Engineer to create the finance solution all businesses love - Tailor made remote policy or relocation package.
Qonto

Senior FullStack Ruby Engineer — We build the testing platform used by companies like Google or Facebook and 50k professional testers around the world.
Global App Testing

Find Ruby Jobs Through Hired — Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It's free for job-seekers.
Hired

📕 Articles & Tutorials

One Row, Many Threads: How to Avoid Database Duplicates in Rails Apps — A primer on avoiding duplicates in database tables that back multi-threaded Rails applications, with practical code examples.

Dmitry Tsepelev

Demystifying Rails Autoloading — Kyle starts with plain, old Ruby require and makes a quick visit to Bundler before explaining this Rails magic.

Kyle Fazzari

Crunchy Bridge: Fully Managed Cloud Postgres — Crunchy Bridge users experience performance improvements, better developer workflow and seamless migration.

Crunchy Bridge sponsor

asdf and Docker for Managing Local Development Dependencies — Docker provides a handy way to get more isolation between local dependencies (such as databases). asdf then provides a way to switch between sets of executables.

Paweł Urbanek

Refactoring: In Defense of Magic — Metaprogramming has a bit of a stigma to it in Ruby, but maybe we’ve overcorrected as a community? Noel thinks so as he sings the praises of metaprogramming and its benefits.

Noel Rappin

Using Hotwire with Rails for a SPA Like Experience — It’s pretty amazing what you can do with the Hotwire gems while writing little to no JavaScript. It’s like a Turbo (ha!) boost to a SPA.

Mike Wilson

A Beginner's Guide to RuboCop in Rails
Prabin Poudel

🛠 Code & Tools

Reek: A High Level 'Code Smell' Detector for Ruby Code — It’ll detect things like odd variable names, unused parameters, overuse of constants and methods, and cohesion problems.

Timo Rößner

Hightop: A Shortcut for Group Count Queries — Adds a top method to Enumberable so works with both plain old arrays or ActiveRecord.

Andrew Kane

ActiveMedian: Median and percentiles for Active Record and More — Not just for Active Record but can work with Mongoid, arrays, and hashes too. With AR it uses SQL with the main database systems for extra efficiency.

Andrew Kane

Build Online Video for Ruby That Just Works

Mux sponsor

Stitches: Create Microservices in Rails with 'Minimal Ceremony' — You get things like API key authentication, API versioning at the router level, and some other niceties added to a Rails app.

Stitch Fix Technology

punks.starter: Algorithmically Generate Your Own Curated Pixel Art — This is fun and the example in the README is worth the price of admission all by itself.

CryptoPunks Not Dead

Store Attribute: ActiveRecord Extension Which Adds Typecasting to Store Accessors
Vladimir Dementyev

rails_respond_to_pb: Rails Middleware to Interface Controllers with Twirp
Brett Dudo

💡 Tip of the Week



Singleton classes (briefly...)

In the past few tips, we've covered ancestors, prepend and include, all in service of learning different ways Ruby allows us to import code into a class. There is one more way to do this, with extend. However, before we jump straight into learning about extend, we first need to understand singleton classes.

Singleton classes are key to Ruby's object model. Let's say we have an instance of a class, and want to define a method on just that instance:

class ExampleClass ; end

example = ExampleClass.new
def example.example_method
  "This is a singleton method"
end

example.example_method
=> "This is a singleton method"

Example.new.example_method
# NoMethodError (undefined method `example_method'
for #<ExampleClass:0x00007f9686858a80>)

We see that our example instance can have its own specially defined method, example_method, which won't work on other instances of ExampleClass. Where is example_method stored though? How does Ruby keep track of it? It's defined on example's singleton class.

Every Ruby object has a singleton class. This allows us to do things like the above: define methods on instances and have them work only for those instances. We also know that classes in Ruby are themselves objects - and so also have singleton classes. This is actually how class methods work under the hood!

Class methods are really instance methods on a class' singleton class. Let's look more deeply:

class ExampleClass
  def self.example_class_method
    "This is a class method"
  end
end

ExampleClass.example_class_method
=> "This is a class method"

ExampleClass.singleton_class.instance_methods(false)
=> [:example_class_method]

Singleton classes are deep enough to deserve a whole series themselves! For now though, we've learned what we need in order to properly understand how extend works next week. Namely, we've learned that class methods are instance methods on the singleton class of a class.

This week’s tip was written by Jemma Issroff.

🕰 ICYMI (Some older stuff that may catch your eye...)