<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Ruby - Tag - MC Squared</title><link>https://mc-squared.dev/tags/ruby/</link><description>Ruby - Tag - MC Squared</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Wed, 12 Aug 2020 09:00:00 +1200</lastBuildDate><atom:link href="https://mc-squared.dev/tags/ruby/" rel="self" type="application/rss+xml"/><item><title>The Day My Code Comments Died</title><link>https://mc-squared.dev/2020/08/the-day-my-code-comments-died/</link><pubDate>Wed, 12 Aug 2020 09:00:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/08/the-day-my-code-comments-died/</guid><description><![CDATA[<p>I remember the day my code comments died. It was 2012, I had moved up from <em>Junior Developer</em> to <em>Developer</em>. My project had started as pure R&amp;D, then moved to prototyping, and finally to production.</p>
<p>As part of the work, I had to develop an algorithm to run over an incoming stream of measurements and detect outliers. Once we had settled in on our approach my supervisor asked me to write it up. You know, the old <em>&ldquo;in case you&rsquo;re hit by a bus tomorrow&rdquo;</em> documentation.</p>]]></description></item><item><title>Benchmarking Ruby on Rails End-to-End Page Performance</title><link>https://mc-squared.dev/2020/07/benchmarking-ruby-on-rails-end-to-end-page-performance/</link><pubDate>Wed, 22 Jul 2020 09:00:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/07/benchmarking-ruby-on-rails-end-to-end-page-performance/</guid><description><![CDATA[<p>There&rsquo;s a lot of information available about ways to improve performance in Ruby on Rails applications. I&rsquo;ve even <a href="/2020/06/ways-to-speed-up-your-ruby-on-rails-apps-performance-without-caching" rel="">written about it myself</a>. But one thing that&rsquo;s often skipped over is exactly how you are supposed to benchmark your performance improvements, particularly if you want to do a full end-to-end speed comparison.</p>
<h2 id="benchmarking-vs-profiling">Benchmarking vs Profiling</h2>
<p>A quick refresher; to profile code (in performance terms) is to figure out <em>which</em> parts of the code are causing a slowdown. This is how you figure out which part of your code needs attention - is it the view, application, or database layer? The <a href="https://github.com/MiniProfiler/rack-mini-profiler" target="_blank" rel="noopener noreffer ">rack-mini-profiler</a> gem is a nice way to add some of this to a Rails application, and you can configure it to produce flamegraphs as well for more detailed analysis.</p>]]></description></item><item><title>Forwarding all arguments with Ruby 2.7</title><link>https://mc-squared.dev/2020/07/forwarding-all-arguments-with-ruby-2-7/</link><pubDate>Tue, 14 Jul 2020 09:00:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/07/forwarding-all-arguments-with-ruby-2-7/</guid><description>&lt;p>Ruby 2.7+ deprecates the ability to automatically translate between a hash and keyword arguments for the last argument in a method invocation. This will be removed in Ruby 3.0 due to some ambiguity in various cases. For a more in-depth rundown, you can check &lt;a href="https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/" target="_blank" rel="noopener noreffer ">the writeup on ruby-lang.org&lt;/a>. This post is more about a simple way to solve deprecation warnings of existing code if you are upgrading to Ruby 2.7.&lt;/p></description></item><item><title>Using Memoization to Speed up Ruby on Rails Applications</title><link>https://mc-squared.dev/2020/06/speeding-up-rails-with-memoization/</link><pubDate>Wed, 24 Jun 2020 09:00:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/06/speeding-up-rails-with-memoization/</guid><description><![CDATA[<h2 id="what-is-memoization">What is Memoization?</h2>
<p>When developing applications we often have methods that run slowly. Perhaps they need to query the database, or hit an external service, both of which can cause them to slow down. We could call the method every time we need that data and just accept the overhead, but if performance is a concern we have some options. For one, we can assign the data to a variable and re-use it, which would speed up the process. While a possible solution, manually managing that variable could quickly become tedious. But, what if instead, the method doing this &ldquo;slow work&rdquo; could just handle that variable for us? This would allow us to call the method in the same way, but have the method save and re-use the data. This is exactly what memoization does.</p>]]></description></item><item><title>Using the Interactor Ruby Gem</title><link>https://mc-squared.dev/2020/06/using-the-interactor-ruby-gem/</link><pubDate>Mon, 01 Jun 2020 23:59:46 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/06/using-the-interactor-ruby-gem/</guid><description><![CDATA[<p>I&rsquo;ve spent a few months now using the <a href="https://github.com/collectiveidea/interactor" target="_blank" rel="noopener noreffer ">interactor</a> gem, both in my day job and also quite extensively in <a href="https://gitarborist.com" target="_blank" rel="noopener noreffer ">GitArborist</a>&rsquo;s code. My first time hearing about &ldquo;interactors&rdquo; was from <a href="https://youtu.be/Nsjsiz2A9mg?t=885" target="_blank" rel="noopener noreffer ">a talk by Robert Martin</a> on clean architecture, essentially saying that an &ldquo;interactor&rdquo; object should encapsulate a single &ldquo;use case&rdquo; or user story, basically the business logic. Therefore, if you look at a list of interactors in the system you can quickly determine what operations that system carries out (in theory).</p>]]></description></item><item><title>Figuring Out How To Assign a Default Wait Time to ActiveJob Jobs</title><link>https://mc-squared.dev/2020/05/figuring-out-how-to-assign-a-default-wait-time-to-activejob-jobs/</link><pubDate>Wed, 06 May 2020 00:09:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/05/figuring-out-how-to-assign-a-default-wait-time-to-activejob-jobs/</guid><description><![CDATA[<p>ActiveJob is Rails&rsquo; way of handling background jobs, providing a common interface around various queuing backends that do the actual processing (Sidekiq, Resque, etc). ActiveJob&rsquo;s interface is pretty simple, you call <code>#perform_later</code> on the job, that job gets added to the queue, and at some point, a worker process will pick it up and execute it.</p>
<p>You can also set various options via the <code>set</code> method, for example:</p>
<div class="code-block open" style="counter-reset: code-block 0">
    <div class="code-header language-ruby">
        <span class="code-title"><i class="arrow fas fa-angle-right" aria-hidden="true"></i></span>
        <span class="ellipses"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
        <span class="copy" title="Copy to clipboard"><i class="far fa-copy" aria-hidden="true"></i></span>
    </div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ruby" data-lang="ruby"><span class="line"><span class="cl">  <span class="no">ThingJob</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="ss">wait</span><span class="p">:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="p">)</span><span class="o">.</span><span class="n">perform_later</span></span></span></code></pre></div></div>
<p>will cause the job to be executed one day from now. In this post, I want to walk through my process of finding a way to set a default wait time for a particular job.</p>]]></description></item><item><title>Delegation: an Alternative to Dependency Injection</title><link>https://mc-squared.dev/2020/04/delegation-an-alternative-to-dependency-injection/</link><pubDate>Wed, 08 Apr 2020 10:00:00 +1300</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/04/delegation-an-alternative-to-dependency-injection/</guid><description><![CDATA[<p>In <em>Object-Oriented Programming</em>, one object will often depend on another object in order to function.</p>
<p>For example, if I create a simple class to run finance reports:</p>
<div class="code-block open" style="counter-reset: code-block 0">
    <div class="code-header language-ruby">
        <span class="code-title"><i class="arrow fas fa-angle-right" aria-hidden="true"></i></span>
        <span class="ellipses"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
        <span class="copy" title="Copy to clipboard"><i class="far fa-copy" aria-hidden="true"></i></span>
    </div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ruby" data-lang="ruby"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">FinanceReport</span>
</span></span><span class="line"><span class="cl">  <span class="k">def</span> <span class="nf">net_income</span>
</span></span><span class="line"><span class="cl">    <span class="no">FinanceApi</span><span class="o">.</span><span class="n">gross_income</span> <span class="o">-</span> <span class="no">FinanceApi</span><span class="o">.</span><span class="n">total_costs</span>
</span></span><span class="line"><span class="cl">  <span class="k">end</span>
</span></span><span class="line"><span class="cl"><span class="k">end</span></span></span></code></pre></div></div>
<p>We can say that <code>FinanceReport</code> <em>depends on</em> <code>FinanceApi</code>, which it uses to pull information from an external payment processor.</p>
<p>But what if we want to hit a different API at some point? Or, more likely, what if we want to test this class without hitting external resources? The most common answer is to use Dependency Injection.</p>]]></description></item><item><title>Reducing Developer Friction With Rails 6’s Actionable Errors</title><link>https://mc-squared.dev/2020/04/reducing-developer-friction-with-rails-6-actionable-errors/</link><pubDate>Wed, 01 Apr 2020 08:00:00 +1300</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/04/reducing-developer-friction-with-rails-6-actionable-errors/</guid><description><![CDATA[<p>One of Rails 6’s new features is <a href="https://github.com/rails/rails/commit/45f1c7a3e16437e517baa6606674f7bbb16dba74" target="_blank" rel="noopener noreffer ">ActionableErrors</a>, which allows you to add buttons to Rails’ default error page to perform certain actions. Out-of-the-box Rails 6 includes one for the common “pending migration” error, where you can hit a button on the page and run the migrations without having to jump back to a terminal.</p>
<h2 id="creating-custom-actionableerrors">Creating Custom ActionableErrors</h2>
<p>To create your own “actionable error” is quite simple. You create an error class that includes <code>ActiveSupport::ActionableError</code>, then you can use the <code>action</code>  method to create the button and its behaviour. Here’s a simple example from Rails itself:</p>]]></description></item><item><title>Running Ruby on Rails 6 on a Raspberry Pi Zero as Easy as Heroku</title><link>https://mc-squared.dev/2019/12/running-ruby-on-rails-6-on-rpi-zero-w/</link><pubDate>Sat, 21 Dec 2019 17:01:29 +1300</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2019/12/running-ruby-on-rails-6-on-rpi-zero-w/</guid><description><![CDATA[<p>In this post, I&rsquo;m going to take a look at what it takes to get the latest version of Ruby on Rails (at the time of writing that is Rails 6.0.0 on Ruby 2.6.3) running on a Rpi Zero W.</p>
<p>First, let&rsquo;s answer the burning question of <em>why</em> would anyone do this:</p>
<ol>
<li>Because I can</li>
<li>I work with Ruby on Rails in my job every day, so I am familiar with it</li>
<li>A learning exercise about what it is like to work with <a href="https://balena.io" target="_blank" rel="noopener noreffer ">balena.io</a></li>
<li>An experiment about what is possible with the <a href="https://balena.io" target="_blank" rel="noopener noreffer ">balena.io</a> infrastructure beyond what they support out-of-the-box</li>
</ol>
<p>Let me also get this out of the way: Ruby-on-Rails is not particularly suited for embedded devices, so I would probably never want to do this in a real-world scenario. balena already offers pre-baked images for Rust, Go, C++, Node, etc. All of which are probably better suited to the job.</p>]]></description></item><item><title>Dissecting Code With Ruby's Caller Method</title><link>https://mc-squared.dev/2019/07/dissecting-code-with-rubys-caller-method/</link><pubDate>Tue, 09 Jul 2019 16:45:26 +1300</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2019/07/dissecting-code-with-rubys-caller-method/</guid><description><![CDATA[<p>Here is a short snippet of code that has become one of my favourite debugging tools recently. It’s not efficient, definitely not elegant, but man is it useful.</p>
<div class="code-block open" style="counter-reset: code-block 0">
    <div class="code-header language-ruby">
        <span class="code-title"><i class="arrow fas fa-angle-right" aria-hidden="true"></i></span>
        <span class="ellipses"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
        <span class="copy" title="Copy to clipboard"><i class="far fa-copy" aria-hidden="true"></i></span>
    </div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ruby" data-lang="ruby"><span class="line"><span class="cl">       <span class="n">location</span> <span class="o">=</span> <span class="nb">caller</span><span class="o">.</span><span class="n">first</span>
</span></span><span class="line"><span class="cl">       <span class="n">location</span> <span class="o">=</span> <span class="n">location</span><span class="o">[</span><span class="mi">0</span><span class="o">...</span><span class="n">location</span><span class="o">.</span><span class="n">rindex</span><span class="p">(</span><span class="s2">&#34;:&#34;</span><span class="p">)</span><span class="o">]</span>
</span></span><span class="line"><span class="cl">       <span class="n">line_number</span> <span class="o">=</span> <span class="n">location</span><span class="o">[</span><span class="p">(</span><span class="n">location</span><span class="o">.</span><span class="n">rindex</span><span class="p">(</span><span class="s2">&#34;:&#34;</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">..-</span><span class="mi">1</span><span class="o">]</span>
</span></span><span class="line"><span class="cl">       <span class="n">file</span> <span class="o">=</span> <span class="n">location</span><span class="o">[</span><span class="mi">0</span><span class="o">...</span><span class="n">location</span><span class="o">.</span><span class="n">rindex</span><span class="p">(</span><span class="s2">&#34;:&#34;</span><span class="p">)</span><span class="o">]</span>
</span></span><span class="line"><span class="cl">       <span class="nb">puts</span> <span class="n">location</span>
</span></span><span class="line"><span class="cl">       <span class="nb">puts</span> <span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="o">[</span><span class="n">line_number</span><span class="o">.</span><span class="n">to_i</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span>
</span></span><span class="line"><span class="cl">       <span class="nb">puts</span> <span class="s2">&#34;</span><span class="si">#{</span><span class="n">values</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">       <span class="nb">puts</span> <span class="s2">&#34;--&#34;</span><span class="o">*</span><span class="mi">20</span></span></span></code></pre></div></div>
<p>But let’s back up a moment, how did I get to this stage?</p>
<h2 id="background">Background</h2>
<p>As part of consultancy work we often inherit applications developed by other Rails shops of varying quality. Previous developers of the code we receive have often used, well, let’s just say “creative” approaches.</p>]]></description></item></channel></rss>