<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Keith Schacht's Weblog: Ruby</title><link href="https://keithschacht.com/" rel="alternate"/><link href="https://keithschacht.com/tags/Ruby.atom" rel="self"/><id>https://keithschacht.com/</id><updated>2025-04-28T15:35:23+00:00</updated><author><name>Keith Schacht</name></author><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-tag" 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-tag</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>Ruby's clamp method reduces conditionals</title><link href="https://keithschacht.com/2025/Feb/24/rubys-clamp-method-reduces-conditionals/#atom-tag" 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-tag</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-tag" 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-tag</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>Ruby Sorbet is too verbose, can the syntax be improved?</title><link href="https://keithschacht.com/2024/Oct/29/ruby-sorbet-is-too-verbose-can-the-syntax-be-improved/#atom-tag" rel="alternate"/><published>2024-10-29T20:36:38+00:00</published><updated>2024-10-29T20:36:38+00:00</updated><id>https://keithschacht.com/2024/Oct/29/ruby-sorbet-is-too-verbose-can-the-syntax-be-improved/#atom-tag</id><summary type="html">
    &lt;p&gt;
Awhile back when I was ramping up on Typescript, I was leaning into the benefits of types. In the Ruby world, Sorbet is the most common way to add types so I gave that a shot. In the end, I ripped it out. I spent more time wrangling type checking then the time it spent catching bugs, and one of the issues was the verbose syntax. But recently I came up with a novel solution to this problem. First, here is the verbose syntax — you end up declaring all your arguments twice:
&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;
sig { params(name: T.untyped, nickname: String).returns(Integer) }
def greet(name, nickname)
  puts "Hello, #{name}! Your nickname: #{nickname}"
  name.length
end
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;It may not seem like a big deal, but every time you change the name of an argument or change the order, you have to do it in two places. Recently I had an idea: could we do it in one line if we used Ruby to redefine how methods are defined? I got this syntax working and threw it up on a gist:
&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
# Two positional arguments. The first, name, is untyped.
# The second, nickname, must be a String

df :greet,  :name, nickname: [String], returns: Integer do
  puts "Hello, #{name}! Your nickname: #{nickname}"
  name.length
end
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://gist.github.com/keithschacht/67d4c0bd523bff067ca20140b8172cbf"&gt;Here is a gist&lt;/a&gt;. It supports positional and named arguments. I got it working with runtime and static analysis. I don't plan to take this experiment any further right now, but maybe it's useful to someone.
&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>Ruby can chain methods and right-assign</title><link href="https://keithschacht.com/2024/Oct/29/ruby-can-chain-methods-and-right-assign/#atom-tag" 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-tag</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>