<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ruby on jpterry.com</title><link>/categories/ruby/</link><description>Recent content in Ruby on jpterry.com</description><generator>Hugo</generator><language>en</language><copyright>© John Terry</copyright><lastBuildDate>Thu, 20 Mar 2025 15:27:18 -0700</lastBuildDate><atom:link href="/categories/ruby/index.xml" rel="self" type="application/rss+xml"/><item><title>Exploring Ruby Ractors</title><link>/posts/2025/03/exploring-ruby-ractors/</link><pubDate>Thu, 20 Mar 2025 15:27:18 -0700</pubDate><guid>/posts/2025/03/exploring-ruby-ractors/</guid><description>&lt;h1 id="ruby-ractors-adventure"&gt;Ruby Ractors Adventure&lt;/h1&gt;
&lt;p&gt;&amp;ldquo;I paid for 10 cores, I&amp;rsquo;m gonna use 10 cores!&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I set out today for a silly explore hoping to waste some CPU with a whole bunch of ractors doing math, but I find that YJIT is pretty amazing.&lt;/p&gt;
&lt;p&gt;Ruby 3.0.0 came out in December 2020, more than 4 years ago now, and with it came &lt;a href="https://docs.ruby-lang.org/en/3.0/ractor_md.html"&gt;Ractors&lt;/a&gt; (and Fiber Scheduler but that&amp;rsquo;s another episode). Ractors promised a way to run truly concurrent things within the same ruby process. Ruby has a problem with lots of things being shared and the GVL essentially protects those shared accesses. With ractors, you can run code that is only allowed to share with other ractors in very specific ways. This allows multiple ractors to run code concurrently, as though there were multiple, per-ractor, GVLs.&lt;/p&gt;</description></item><item><title>passing objects to methods as blocks in ruby</title><link>/2012/03/07/passing-objects-to-methods-as-blocks-in-ruby/</link><pubDate>Wed, 07 Mar 2012 00:00:00 +0000</pubDate><guid>/2012/03/07/passing-objects-to-methods-as-blocks-in-ruby/</guid><description>&lt;p&gt;Lambda&amp;rsquo;s can come in handy when you need to apply a similar mutation to a collection of objects.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example of a simple operation but the block can be as complicated or generic as you need.
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"&gt;&lt;code class="language-ruby" data-lang="ruby"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ints_to_floats &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; lambda { &lt;span style="color:#ff7b72;font-weight:bold"&gt;|&lt;/span&gt;i&lt;span style="color:#ff7b72;font-weight:bold"&gt;|&lt;/span&gt; i&lt;span style="color:#ff7b72;font-weight:bold"&gt;.&lt;/span&gt;to_f }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;(&lt;span style="color:#a5d6ff"&gt;0&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;..&lt;/span&gt;&lt;span style="color:#a5d6ff"&gt;5&lt;/span&gt;)&lt;span style="color:#ff7b72;font-weight:bold"&gt;.&lt;/span&gt;map(&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;amp;&lt;/span&gt;ints_to_floats)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#8b949e;font-style:italic"&gt;#=&amp;gt; [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The urnary ampersand calls the :symbol&amp;rsquo;s #to_proc method and passes it as a block to the method.&lt;/p&gt;
&lt;p&gt;In ruby 1.9.2 and monkey patched in by rails, Symbol#to_proc is defined as:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"&gt;&lt;code class="language-ruby" data-lang="ruby"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#79c0ff;font-weight:bold"&gt;Proc&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;.&lt;/span&gt;new { &lt;span style="color:#ff7b72;font-weight:bold"&gt;|*&lt;/span&gt;args&lt;span style="color:#ff7b72;font-weight:bold"&gt;|&lt;/span&gt; args&lt;span style="color:#ff7b72;font-weight:bold"&gt;.&lt;/span&gt;shift&lt;span style="color:#ff7b72;font-weight:bold"&gt;.&lt;/span&gt;__send__(self, &lt;span style="color:#ff7b72;font-weight:bold"&gt;*&lt;/span&gt;args) }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;</description></item></channel></rss>