<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Keith Schacht's Weblog: Today I Learned</title><link href="https://keithschacht.com/" rel="alternate"/><link href="https://keithschacht.com/series/til.atom" rel="self"/><id>https://keithschacht.com/</id><updated>2025-04-28T15:36:38+00:00</updated><author><name>Keith Schacht</name></author><entry><title>with_defaults is a better named alias</title><link href="https://keithschacht.com/2025/Apr/28/with_defaults-is-a-better-named-alias/#atom-series" rel="alternate"/><published>2025-04-28T15:36:38+00:00</published><updated>2025-04-28T15:36:38+00:00</updated><id>https://keithschacht.com/2025/Apr/28/with_defaults-is-a-better-named-alias/#atom-series</id><summary type="html">
    &lt;p&gt;`with_defaults` is the same as `reverse_merge` in rails but much better named.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>Bundler can be configured to auto install</title><link href="https://keithschacht.com/2025/Apr/28/bundler-can-be-configured-to-auto-install/#atom-series" rel="alternate"/><published>2025-04-28T15:35:23+00:00</published><updated>2025-04-28T15:35:23+00:00</updated><id>https://keithschacht.com/2025/Apr/28/bundler-can-be-configured-to-auto-install/#atom-series</id><summary type="html">
    &lt;p&gt;No more pulling down changes to a project and realizing I need to run `bundle install`. Run this command once on a dev machine and it'll auto install whenever new gems are needed: `bundle config --global auto_install true`.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Ruby"&gt;Ruby&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Ruby"/></entry><entry><title>React in Rails is gaining momentum with Inertia</title><link href="https://keithschacht.com/2025/Apr/14/react-in-rails-is-gaining-momentum-with-inertia/#atom-series" rel="alternate"/><published>2025-04-14T23:44:45+00:00</published><updated>2025-04-14T23:44:45+00:00</updated><id>https://keithschacht.com/2025/Apr/14/react-in-rails-is-gaining-momentum-with-inertia/#atom-series</id><summary type="html">
    &lt;p&gt;I am seeing more activity around an elegant way to utilize React in Rails. I plan to try this out soon and &lt;a href="https://inertia-rails.dev/awesome#articles"&gt;these articles&lt;/a&gt; are a good starting point. There is even a &lt;a href="https://github.com/skryukov/inertia-rails-shadcn-starter"&gt;shadcn starter project&lt;/a&gt; which is part of my motivation.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>Ruby's clamp method reduces conditionals</title><link href="https://keithschacht.com/2025/Feb/24/rubys-clamp-method-reduces-conditionals/#atom-series" rel="alternate"/><published>2025-02-24T15:19:46+00:00</published><updated>2025-02-24T15:19:46+00:00</updated><id>https://keithschacht.com/2025/Feb/24/rubys-clamp-method-reduces-conditionals/#atom-series</id><summary type="html">
    &lt;p&gt;
You can constrain a variable to be within a range using clamp:

&lt;pre&gt;
5.clamp(1, 10)  # =&gt; 5
-3.clamp(1, 10) # =&gt; 1
15.clamp(1, 10) # =&gt; 10
&lt;/pre&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Ruby"&gt;Ruby&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Ruby"/></entry><entry><title>Regex within string square brackets</title><link href="https://keithschacht.com/2025/Feb/24/regex-within-string-square-brackets/#atom-series" rel="alternate"/><published>2025-02-24T15:16:30+00:00</published><updated>2025-02-24T15:16:30+00:00</updated><id>https://keithschacht.com/2025/Feb/24/regex-within-string-square-brackets/#atom-series</id><summary type="html">
    &lt;p&gt;
I've always known that Ruby supports ranges within a string's square brackets. So in addition to the simple `name[4]` I can do `name[2..4]` and `name[2...]` and `name[-1]`. But I just learned you can put regex in the square brackets for more dynamic substring selectors: `"me+abc123@email.com"[/.+\+(.+)@(.+)/, 2]` will give you the subdomain of the email address. It even works with named capture groups! `"123.45"[/(?&amp;lt;dollars&amp;gt;\d+)\.(?&amp;lt;centers&amp;gt;\d+)/, :dollars]` will give you the dollars. The article has a few other notable examples.
&lt;/p&gt;&lt;p&gt;
&lt;a href="https://github.com/jbranchaud/til/blob/master/ruby/extract-capture-group-matches-with-string-slices.md"&gt;via&lt;/a&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Ruby"&gt;Ruby&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Ruby"/></entry><entry><title>Elegant solution to versioning an API</title><link href="https://keithschacht.com/2025/Feb/24/elegant-solution-to-versioning-an-api/#atom-series" rel="alternate"/><published>2025-02-24T15:13:14+00:00</published><updated>2025-02-24T15:13:14+00:00</updated><id>https://keithschacht.com/2025/Feb/24/elegant-solution-to-versioning-an-api/#atom-series</id><summary type="html">
    &lt;p&gt;
The couple times I've done API versioning it was with /v1/ or v1.domain.com. Both of these are the generally recommended pattern within rails. This article outlines a querystring based approach to API versioning and a much more flexible implementation strategy that avoids a lot of duplication and code maintenance.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://petr.codes/blog/rails/flexible-api-versioning-with-rails"&gt;via&lt;/a&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>pick is the single-value version of pluck in Rails</title><link href="https://keithschacht.com/2024/Nov/26/pick-is-the-single-value-version-of-pluck-in-rails/#atom-series" rel="alternate"/><published>2024-11-26T23:24:00+00:00</published><updated>2024-11-26T23:24:00+00:00</updated><id>https://keithschacht.com/2024/Nov/26/pick-is-the-single-value-version-of-pluck-in-rails/#atom-series</id><summary type="html">
    &lt;p&gt;Instead of doing &lt;pre&gt;Messages.pluck(:user_id).first&lt;/pre&gt; you can do &lt;pre&gt;Messages.pick(:user_id)&lt;/pre&gt;&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>Rails ActiveRecord has a .sole method</title><link href="https://keithschacht.com/2024/Nov/26/rails-activerecord-has-a-sole-method/#atom-series" rel="alternate"/><published>2024-11-26T23:15:43+00:00</published><updated>2024-11-26T23:15:43+00:00</updated><id>https://keithschacht.com/2024/Nov/26/rails-activerecord-has-a-sole-method/#atom-series</id><summary type="html">
    &lt;p&gt;I just learned about `sole` and `find_sole_by`. They ensure that only a single record is returned and will raise if there is something other than one. It's the equivalent of:

&lt;pre&gt;
api_keys = user.api_keys.where(key: key)

if api_keys.length == 1
  api_key = api_keys.first
elsif api_keys.length == 0
  raise "No records found!"
else
  raise "More than one matching record found!"
end
&lt;/pre&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>NASA's Dragonfly probe to Saturn's Titan in 2028</title><link href="https://keithschacht.com/2024/Nov/26/nasa-dragonfly-probe-to-saturn-titan-in-2028/#atom-series" rel="alternate"/><published>2024-11-26T14:58:00+00:00</published><updated>2024-11-26T14:58:00+00:00</updated><id>https://keithschacht.com/2024/Nov/26/nasa-dragonfly-probe-to-saturn-titan-in-2028/#atom-series</id><summary type="html">
    &lt;p&gt;In 2028 NASA is launching a spacecraft to Saturn's moon Titan to study its habitability in a mission named Dragonfly. It will land in 2034. They're sending a drone powered by a radioisotope thermoelectric generator so they can visit a variety of sites. Titan is notable because it's believed to have a similar chemistry to ancient earth. Past probes have measured the contents of the atmosphere and confirmed it contains a mix of hydrocarbons. The surface contains a lot of water ice and scientists believe there is a water ocean in the interior of the moon.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/space"&gt;space&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="space"/></entry><entry><title>Rails migrations can include an up_only part</title><link href="https://keithschacht.com/2024/Oct/29/rails/#atom-series" rel="alternate"/><published>2024-10-29T20:29:09+00:00</published><updated>2024-10-29T20:29:09+00:00</updated><id>https://keithschacht.com/2024/Oct/29/rails/#atom-series</id><summary type="html">
    &lt;p&gt;
When writing a migration in Rails and you need to do one thing which can't be automatically reversed, I always convert a &lt;b&gt;def change&lt;/b&gt; to up and down. It turns out you can slip in an &lt;b&gt;up_only&lt;/b&gt; block:

&lt;pre&gt;
class AddFieldsToUsers &amp;lt; 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
&lt;/pre&gt;

&lt;a href="https://x.com/RubyCademy/status/1849366408150327411"&gt;from RubyCademy&lt;/a&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Rails"&gt;Rails&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Rails"/></entry><entry><title>Ruby can chain methods and right-assign</title><link href="https://keithschacht.com/2024/Oct/29/ruby-can-chain-methods-and-right-assign/#atom-series" rel="alternate"/><published>2024-10-29T20:23:35+00:00</published><updated>2024-10-29T20:23:35+00:00</updated><id>https://keithschacht.com/2024/Oct/29/ruby-can-chain-methods-and-right-assign/#atom-series</id><summary type="html">
    &lt;p&gt;
I just learned that ruby supports re-writing this:
&lt;pre&gt;
total = plus_shipping(with_taxes(subtotal(items)))
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;As:
&lt;pre&gt;
subtotal(items).then { |subtotal| with_taxes(subtotal) }.then { |total| plus_shipping(total) } =&gt; total
&lt;/pre&gt;

&lt;a href="https://ruby.social/@davetron5000/113362613406267986"&gt;from davetron&lt;/a&gt;
&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://keithschacht.com/tags/Ruby"&gt;Ruby&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="Ruby"/></entry></feed>