Keith Schacht’s Weblog

Subscribe

Series: Today I Learned

A collection of concise write-ups on small things I learn day to day across a variety of languages and technologies. These are things that don’t really warrant a full blog post.

Atom feed

💡 TIL: Ruby can chain methods and right-assign #

I just learned that ruby supports re-writing this:

total = plus_shipping(with_taxes(subtotal(items)))

As:

subtotal(items).then { |subtotal| with_taxes(subtotal) }.then { |total| plus_shipping(total) } => total
from davetron

💡 TIL: Rails migrations can include an up_only part #

When writing a migration in Rails and you need to do one thing which can’t be automatically reversed, I always convert a def change to up and down. It turns out you can slip in an up_only block:

class AddFieldsToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string

    up_only do
      User.delete_all
    end
  end
end
from RubyCademy