<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://blog.alex-miller.co/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blog.alex-miller.co/" rel="alternate" type="text/html" /><updated>2020-01-23T23:02:27+00:00</updated><id>https://blog.alex-miller.co/feed.xml</id><title type="html">It’s alex’s blog</title><subtitle>This is Alex Miller's blog.  It contains posts on topics like Ruby, Angular, Elixir and Emacs.
</subtitle><author><name>Alex Miller</name></author><entry><title type="html">Rails Route Globbing</title><link href="https://blog.alex-miller.co/rails/2020/01/21/rails-route-globbing.html" rel="alternate" type="text/html" title="Rails Route Globbing" /><published>2020-01-21T05:00:00+00:00</published><updated>2020-01-21T05:00:00+00:00</updated><id>https://blog.alex-miller.co/rails/2020/01/21/rails-route-globbing</id><content type="html" xml:base="https://blog.alex-miller.co/rails/2020/01/21/rails-route-globbing.html">&lt;p&gt;Using a wildcard syntax (&lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt;) when defining routes in &lt;code class=&quot;highlighter-rouge&quot;&gt;routes.rb&lt;/code&gt;, we can slurp 
up URL segments and make them available in our controller &lt;code class=&quot;highlighter-rouge&quot;&gt;params&lt;/code&gt;. Here’s an 
example:&lt;/p&gt;

&lt;h3 id=&quot;globbing-example&quot;&gt;Globbing Example&lt;/h3&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# config/routes.rb&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'photos/*path'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'photos_controller#index'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now in the &lt;code class=&quot;highlighter-rouge&quot;&gt;PhotosController&lt;/code&gt; you can access the globbed segments in the params 
hash. Let’s say the request comes in on &lt;code class=&quot;highlighter-rouge&quot;&gt;photos/familiy/cousins&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 'family/counsins'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;catch-all-route&quot;&gt;Catch-all Route&lt;/h3&gt;
&lt;p&gt;I can’t think of many reasons why we’d want to use route globbing. 
If you know some good used cases, please comment below. A catch-all route is one
possible use case.&lt;/p&gt;

&lt;p&gt;Rails will use the first defined route starting from the top of &lt;code class=&quot;highlighter-rouge&quot;&gt;routes.rb&lt;/code&gt; that 
matches a request URL. So, if a wildcard, globbed route is defined last, this will 
be used as a catch-all route for a request that doesn’t match any of the 
previously defined routes.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# config/routes.rb at the bottom&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*path'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'static_pages_controller#not_found'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;via: :all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Any request that comes in on a URL that doesn’t match any other previously 
defined route, will be routed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;StaticPagesController&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;not_found&lt;/code&gt; 
action. The &lt;code class=&quot;highlighter-rouge&quot;&gt;via: :all&lt;/code&gt; ensures that it will match against any HTTP verb.
Alternatively, we can be explicit about which HTTP verbs we want to match by
using any array of verbs: &lt;code class=&quot;highlighter-rouge&quot;&gt;via: [:get, :edit]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Or if you’re too lazy to make a controller and view, you can give it a proc or 
lambda.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# config/routes.rb at the bottom&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*path'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;404&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Not Found'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;via: :all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Side Note&lt;/strong&gt;
This works because routes are losely coupled to controllers. Hence, a request
can be handled any object that responds to &lt;code class=&quot;highlighter-rouge&quot;&gt;.call&lt;/code&gt; and takes a single argument.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://guides.rubyonrails.org/routing.html#http-verb-constraints&quot;&gt;Verb Contraints&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments&quot;&gt;Route Globbing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;If you’re into code speluking, and want to deep dive into how route globbing is
implemented, &lt;a href=&quot;https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L230-L240&quot;&gt;this method in the Rails router source code&lt;/a&gt; 
might be a good place to start.&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">Using a wildcard syntax (*) when defining routes in routes.rb, we can slurp up URL segments and make them available in our controller params. Here’s an example:</summary></entry><entry><title type="html">Include Arbitrary Files in an Elixir Project</title><link href="https://blog.alex-miller.co/elixir/2018/05/26/including-arbitrary-files-in-elixir-project.html" rel="alternate" type="text/html" title="Include Arbitrary Files in an Elixir Project" /><published>2018-05-26T13:10:00+00:00</published><updated>2018-05-26T13:10:00+00:00</updated><id>https://blog.alex-miller.co/elixir/2018/05/26/including-arbitrary-files-in-elixir-project</id><content type="html" xml:base="https://blog.alex-miller.co/elixir/2018/05/26/including-arbitrary-files-in-elixir-project.html">&lt;h1 id=&quot;include-arbitrary-files-in-elixir-project&quot;&gt;Include Arbitrary Files in Elixir Project&lt;/h1&gt;

&lt;p&gt;There might be occasions in which we would want to include arbitrary files in our
Elixir projects.  Maybe we need to include executable.  Or perhaps we’re
building some kind of renderer and need some assets.&lt;/p&gt;

&lt;p&gt;Such files should go in the optional&lt;code class=&quot;highlighter-rouge&quot;&gt;priv/&lt;/code&gt; directory in the project root.
Assets placed here will be available during the application runtime.
Executables are supposed to go in &lt;code class=&quot;highlighter-rouge&quot;&gt;priv/bin&lt;/code&gt;.  See
the &lt;a href=&quot;http://erlang.org/doc/design_principles/applications.html#id81839&quot;&gt;Erlang application docs&lt;/a&gt;
for more details.&lt;/p&gt;

&lt;h3 id=&quot;accessing-files-in-priv-at-runtime&quot;&gt;Accessing Files in &lt;code class=&quot;highlighter-rouge&quot;&gt;priv/&lt;/code&gt; at Runtime&lt;/h3&gt;
&lt;p&gt;Let’s say you have &lt;code class=&quot;highlighter-rouge&quot;&gt;priv/my_file.txt&lt;/code&gt; and need to access it during the
application’s runtime.  We can do so with the following code:&lt;/p&gt;

&lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;no&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;priv_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;my_file.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;in-escripts&quot;&gt;In escripts&lt;/h3&gt;
&lt;p&gt;Escripts do not support loading artifacts in the &lt;code class=&quot;highlighter-rouge&quot;&gt;priv/&lt;/code&gt; directory at runtime.
To work around this issue, we can store the file or file contents in a module
attribute at compile time, as Jose Valim explains in
&lt;a href=&quot;https://elixirforum.com/t/is-it-possible-to-include-resource-files-when-packaging-my-project-using-mix-escript/730&quot;&gt;this ElixirForm post&lt;/a&gt;.
Here’s an example:&lt;/p&gt;

&lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SomeModule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;@my_file_contents&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read!&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;priv/my_file.txt&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_file_contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;@my_file_contents&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, when we compile our project we’ll have the contents of &lt;code class=&quot;highlighter-rouge&quot;&gt;my_file.txt&lt;/code&gt;
stored in the module attribute &lt;code class=&quot;highlighter-rouge&quot;&gt;@my_file_contents&lt;/code&gt; which is accessible in escripts.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://erlang.org/doc/design_principles/applications.html#id81839&quot;&gt;Erlang Application Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">Include Arbitrary Files in Elixir Project</summary></entry><entry><title type="html">Correlated to Complete: A SQL Subquery Refactor</title><link href="https://blog.alex-miller.co/sql/2018/02/17/correlated-to-complete-sql-refactor.html" rel="alternate" type="text/html" title="Correlated to Complete: A SQL Subquery Refactor" /><published>2018-02-17T14:29:00+00:00</published><updated>2018-02-17T14:29:00+00:00</updated><id>https://blog.alex-miller.co/sql/2018/02/17/correlated-to-complete-sql-refactor</id><content type="html" xml:base="https://blog.alex-miller.co/sql/2018/02/17/correlated-to-complete-sql-refactor.html">&lt;p&gt;I recently constructed a SQL query that preformed rather poorly.  The query
involved selecting about 10,000 records with a complicted subquery.  It was
noticeably slow and needed to be improved. It was easy to see that the subquery
was running for each record, like a loop. This is known as a correlated subquery.
The refactor involved removing the loop and executing the subquery just once.
Here’s how it went down:&lt;/p&gt;

&lt;h2 id=&quot;correlated-subquery&quot;&gt;Correlated Subquery&lt;/h2&gt;
&lt;p&gt;A correlated subquery is like a loop.  It will executed once for each row in the
outer query.  Thus, correlated subqueries can be costly.  Here is a somewhat
contrived example using Postgres:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AVG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relative_velocity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'miles_per_hour'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close_approaches&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close_approaches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asteroid_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocity&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asteroids&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here, we want the average &lt;code class=&quot;highlighter-rouge&quot;&gt;relative_velocity&lt;/code&gt; of the close approaches for each
asteroid.  The &lt;code class=&quot;highlighter-rouge&quot;&gt;avg_velocity&lt;/code&gt; subquery is correlated since it runs for each
selected asteroid in the outer query.&lt;/p&gt;

&lt;p&gt;If you’re wondering about the &lt;code class=&quot;highlighter-rouge&quot;&gt;relative_velocity-&amp;gt;&amp;gt; 'miles_per_hour'&lt;/code&gt; syntax,
that’s just PostgreSQL &lt;a href=&quot;https://www.postgresql.org/docs/current/static/functions-json.html&quot;&gt;JSON operator&lt;/a&gt;
for selecting the &lt;code class=&quot;highlighter-rouge&quot;&gt;miles_per_hour&lt;/code&gt; key from the &lt;code class=&quot;highlighter-rouge&quot;&gt;relative_velocity&lt;/code&gt; JSON field.&lt;/p&gt;

&lt;p&gt;To demonstrate the expense of such a query, for let’s say 10,000 asteroids
each with several close approaches, we can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN&lt;/code&gt; function on the
query.  Using &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN&lt;/code&gt; will give us a ton of information about the full
execution plan, but we’re just interested in the &lt;code class=&quot;highlighter-rouge&quot;&gt;Total Cost&lt;/code&gt; for this
demonstration. We’ll use the &lt;code class=&quot;highlighter-rouge&quot;&gt;Total Cost&lt;/code&gt; as a benchmark comparison when
optimizing to use a complete subquery below.&lt;/p&gt;

&lt;p&gt;By the way, I am using &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN (FORMAT JSON)&lt;/code&gt; because I find the format a bit
easier to read. Here are the results for the above query:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
   &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Node Type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Sort&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Parallel Aware&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Startup Cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;236908.35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Total Cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;236933.34&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan Rows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9993&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan Width&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;61&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Sort Key&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;((SubPlan 1)) DESC&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plans&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There’s a lot more to this, but I’m only showing the first part with
the &lt;code class=&quot;highlighter-rouge&quot;&gt;Total Cost&lt;/code&gt;, which is &lt;code class=&quot;highlighter-rouge&quot;&gt;236933.34&lt;/code&gt;.  Let’s see how much lower our cost can
be when we refactor this query to get rid of the correlated subquery in favor
of a complete subquery.&lt;/p&gt;

&lt;h2 id=&quot;complete-subquery&quot;&gt;Complete Subquery&lt;/h2&gt;

&lt;p&gt;A complete subquery (also just known as a subquery), can manifest itself as a
&lt;code class=&quot;highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;.  We’ll take this &lt;code class=&quot;highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; approach and refactor our query to join
asteroids to the result of a subquery like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;avg_velocity&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asteroids&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asteroid_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AVG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relative_velocity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'miles_per_hour'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)::&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocity&lt;/span&gt;
           &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close_approaches&lt;/span&gt;
           &lt;span class=&quot;k&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asteroid_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocities&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asteroid_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asteroids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_velocity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here, in addition to the &lt;code class=&quot;highlighter-rouge&quot;&gt;avg_velocity&lt;/code&gt; computation, our subquery also selects
the &lt;code class=&quot;highlighter-rouge&quot;&gt;asteroid_id&lt;/code&gt; from &lt;code class=&quot;highlighter-rouge&quot;&gt;close_approaches&lt;/code&gt;, since we use it to join back to
asteroids and group the average velocities per asteroid. The important thing here
is that the subquery runs only once, rather than for each asteroid like the first
example.  All we need to do is join the &lt;code class=&quot;highlighter-rouge&quot;&gt;asteroids&lt;/code&gt; to the result of the subquery.&lt;/p&gt;

&lt;p&gt;Overall, re-writing the query this way makes it a bit more verbose and complex,
but lets see how it performs:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN (FORMAT JSON)&lt;/code&gt; …&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
   &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Node Type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Sort&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Parallel Aware&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Startup Cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;8575.66&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Total Cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;8600.67&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan Rows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10007&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plan Width&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;61&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Sort Key&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;((SubPlan 1)) DESC&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;Plans&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the first example the &lt;code class=&quot;highlighter-rouge&quot;&gt;Total Cost&lt;/code&gt; was &lt;code class=&quot;highlighter-rouge&quot;&gt;236933.34&lt;/code&gt;.  After refactoring to a
complete subquery we’re down to a &lt;code class=&quot;highlighter-rouge&quot;&gt;Total Cost&lt;/code&gt; of &lt;code class=&quot;highlighter-rouge&quot;&gt;8600.67&lt;/code&gt;. That’s over 27
times cheaper! 🙂&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Correlated subqueries aren’t always bad.  Sometimes they may perform well enough.
But if you’re finding it too slow, look to refactoring the query to use a
single subquery on which to join.  Remember to use &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN&lt;/code&gt; for comparison
bench-marking.&lt;/p&gt;

&lt;h1 id=&quot;resources&quot;&gt;Resources&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Correlated_subquery&quot;&gt;Correlated Subquery on Wikipedia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/static/sql-explain.html&quot;&gt;PostgreSQL EXPLAIN Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">I recently constructed a SQL query that preformed rather poorly. The query involved selecting about 10,000 records with a complicted subquery. It was noticeably slow and needed to be improved. It was easy to see that the subquery was running for each record, like a loop. This is known as a correlated subquery. The refactor involved removing the loop and executing the subquery just once. Here’s how it went down:</summary></entry><entry><title type="html">Scope the Monkey: Refinements in Ruby</title><link href="https://blog.alex-miller.co/ruby/2017/07/22/scope-the-monkey.html" rel="alternate" type="text/html" title="Scope the Monkey: Refinements in Ruby" /><published>2017-07-22T05:00:00+00:00</published><updated>2017-07-22T05:00:00+00:00</updated><id>https://blog.alex-miller.co/ruby/2017/07/22/scope-the-monkey</id><content type="html" xml:base="https://blog.alex-miller.co/ruby/2017/07/22/scope-the-monkey.html">&lt;p&gt;&lt;img src=&quot;/assets/images/monkey-patch.jpg&quot; alt=&quot;monkey patch&quot; /&gt;
This post is about limiting the scope of “monkey patches” with Ruby’s Refinements.
This is certainly nothing new - there are numerous blogs posts and talks on this topic
if you just Google around a bit.  Despite Refinements having been around in Ruby since
version 2.0, it’s not something we see very often.  For that reason, it can help revist
the topic in order to get re-acquainted with it, or learn something new.&lt;/p&gt;

&lt;h2 id=&quot;the-problem-with-rubys-open-classes&quot;&gt;The Problem with Ruby’s Open Classes&lt;/h2&gt;
&lt;p&gt;As usual, to understand a thing, we need to understand the problem that thing is 
meant to solve.&lt;/p&gt;

&lt;h1 id=&quot;open-classes&quot;&gt;Open Classes&lt;/h1&gt;
&lt;p&gt;The Open Class technique, more commonly known as ‘monkey patching’, is a meta programming technique 
that lets the developer add new methods (or change existing ones) to a class at run-time.
For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;randomize&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In this example, the &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; class is re-opened and the &lt;code class=&quot;highlighter-rouge&quot;&gt;randomize&lt;/code&gt; method is added.
Doing this affects every existing instance of &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; and all new instances going forward.
This works because instance methods are stored in the class object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'monkey'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;scramble&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;message&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; undefined method `scramble' for &quot;monkey&quot;:String&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;scramble&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;scramble&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; omknye&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h1 id=&quot;so-whats-the-problem-with-this&quot;&gt;So, what’s the problem with this?&lt;/h1&gt;
&lt;p&gt;“Monkey patching” is global. The above change affects every &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; object in the
entire application. Some things to consider:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Will it conflict with a 3rd party library?&lt;/li&gt;
  &lt;li&gt;Could we accidentally override an existing method?&lt;/li&gt;
  &lt;li&gt;Will our patch remain compatible with future versions of Ruby?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;refine&quot;&gt;Refine&lt;/h2&gt;
&lt;p&gt;We can limit the scope of our “monkey patches” by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;refine&lt;/code&gt; inside a module
definition:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;StringExtensions&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;refine&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;scramble&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;A Refinement is not active just by defining it.  To activate a Refinement, it must be done
explicitly with &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;using&quot;&gt;Using&lt;/h2&gt;
&lt;p&gt;To activate Refinement call &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;StringExtensions&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can do this inside a module or class so that our patch is only active inside a module
or class definition.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Scramble&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;StringExtensions&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;scramble&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;Scramble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'monkey'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; knmeyo&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;A Refinement is active is two places:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;refine&lt;/code&gt; block itself&lt;/li&gt;
  &lt;li&gt;Starting at the place in the code where &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt; was called until the end of the definition if inside a module or
class.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;some-gotchas&quot;&gt;Some Gotchas&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Methods already called in a definition are not Refined after calling &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt;.  Here is an example:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Calculate&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add1_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;one&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CalculateExtensions&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;refine&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Calculate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;one&lt;/span&gt;
      &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CalculateExtensions&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Calculate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;one&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; 1.0&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Calculate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add1_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After we call &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt;, it’s reasonable that one would expect &lt;code class=&quot;highlighter-rouge&quot;&gt;Calculate.new.add1_to(2)&lt;/code&gt; to return &lt;code class=&quot;highlighter-rouge&quot;&gt;3.0&lt;/code&gt; 
due to coercion, but we’re actually still adding &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; and not &lt;code class=&quot;highlighter-rouge&quot;&gt;1.0&lt;/code&gt;.  That’s because the call to &lt;code class=&quot;highlighter-rouge&quot;&gt;one&lt;/code&gt; inside 
&lt;code class=&quot;highlighter-rouge&quot;&gt;add1_to(num)&lt;/code&gt; method on line 3 happens before the call to &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt; on line 19.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;calling &lt;code class=&quot;highlighter-rouge&quot;&gt;using&lt;/code&gt; directly in IRB at the moment doesn’t work.  You can learn more about 
this &lt;a href=&quot;https://bugs.ruby-lang.org/issues/9580&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Refinements are an easy way to limit the scope of our “monkey patches”, thereby making them much
safer to implement. We can avoid unexpected results that can come with making global changes to our code, 
yet still take advantage of Open Classes.&lt;/p&gt;
&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://ruby-doc.org/core-2.4.1/doc/syntax/refinements_rdoc.html&quot;&gt;Ruby docs on Refinements&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">This post is about limiting the scope of “monkey patches” with Ruby’s Refinements. This is certainly nothing new - there are numerous blogs posts and talks on this topic if you just Google around a bit. Despite Refinements having been around in Ruby since version 2.0, it’s not something we see very often. For that reason, it can help revist the topic in order to get re-acquainted with it, or learn something new.</summary></entry><entry><title type="html">YASnippet Template System for Emacs</title><link href="https://blog.alex-miller.co/emacs/spacemacs/2017/05/28/yasnippets.html" rel="alternate" type="text/html" title="YASnippet Template System for Emacs" /><published>2017-05-28T05:00:00+00:00</published><updated>2017-05-28T05:00:00+00:00</updated><id>https://blog.alex-miller.co/emacs/spacemacs/2017/05/28/yasnippets</id><content type="html" xml:base="https://blog.alex-miller.co/emacs/spacemacs/2017/05/28/yasnippets.html">&lt;h2 id=&quot;crank-out-boiler-plate-code-like-a-boss-with-yasnippet&quot;&gt;Crank Out Boiler Plate Code Like a Boss with YASnippet&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/yasnippet_angular_component_demo.gif&quot; alt=&quot;YASnippet Demo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let me explain the above demo.&lt;/p&gt;

&lt;p&gt;I sometimes work in Angular which happens to have a moderate amount
of boiler plate code.  For example, creating a new component means 
importing the &lt;code class=&quot;highlighter-rouge&quot;&gt;Component&lt;/code&gt; decorator, passing in a config object with 
metadata to the component decorator and then exporting the class so 
it can be imported elsewhere.&lt;/p&gt;

&lt;p&gt;In this example, I just type &lt;code class=&quot;highlighter-rouge&quot;&gt;comp&lt;/code&gt; followed by &lt;code class=&quot;highlighter-rouge&quot;&gt;M-/&lt;/code&gt; to expand 
out the component (I’m using Spacemacs. In Emacs, use &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; to expand). 
The cursor is automically dropped at the position where I need to name 
the component’s selector (line 4). As I type the selector name, it is 
copied and appended &lt;code class=&quot;highlighter-rouge&quot;&gt;styleUrls&lt;/code&gt; file name (line 5), as well as camel cased 
on the export line (line 8). When done with the selector name, I hit &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; 
which places me on line 6 where I can add the template.  Now I am ready 
to build my component. Let’s see how this works.&lt;/p&gt;

&lt;p&gt;Since I’m using Spacemacs my examples use &lt;code class=&quot;highlighter-rouge&quot;&gt;SPC SPC&lt;/code&gt;, which for emacs is &lt;code class=&quot;highlighter-rouge&quot;&gt;M-x&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;creating-a-snippet&quot;&gt;Creating a Snippet&lt;/h2&gt;
&lt;h1 id=&quot;where-are-snippets-stored&quot;&gt;Where are snippets stored?&lt;/h1&gt;
&lt;p&gt;First a quick note on where snippets are stored.  When you save a YASnippet,
it’s going to be placed in &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.emacs.d/private/snippets/&lt;/code&gt; in a sub directory
that corresponds to the mode the snippet is for. For exmaple, the Typescript 
snippet in the demo above is in &lt;code class=&quot;highlighter-rouge&quot;&gt;.emacs.d/private/snippets/typescript/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Just remember that the stuff you put in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.emacs.d/private&lt;/code&gt; is git ignored.
I like to manage this folder the way I do with my dotfiles so they get are added 
to version control.&lt;/p&gt;
&lt;h1 id=&quot;create-the-template&quot;&gt;Create the template&lt;/h1&gt;
&lt;p&gt;I like to start in the file type of the mode for which I am building the snippet (in this case a &lt;code class=&quot;highlighter-rouge&quot;&gt;.ts&lt;/code&gt; file).
That way the snippet will automically be placed in the right mode directory. For me,
this happens naturally because I usually get inspired to make a snippet while coding.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;SPC SPC yas-new-snippet&lt;/code&gt; creates a new empty snippet that looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mode:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;snippet&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Angular&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;component&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;key:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;comp&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;name&lt;/code&gt; is the description of the snippet - usefull when listing snippets with &lt;code class=&quot;highlighter-rouge&quot;&gt;SPC i s&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;key&lt;/code&gt; is the string you want to type that will be expanded with &lt;code class=&quot;highlighter-rouge&quot;&gt;M-/&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;everthing below the metadata block (line 4) is where you write the snippet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a an simple example snippet that justs adds a Angular component import line:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mode:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;snippet&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Angular&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;component&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;key:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;comp&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'@angular/core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When your finished with the snippet, just &lt;code class=&quot;highlighter-rouge&quot;&gt;SPC f s&lt;/code&gt; to save it in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.emacs.d/private/snippets/&lt;/code&gt; 
directory. Recall from &lt;a href=&quot;#where-are-snippets-stored&quot;&gt;above&lt;/a&gt; that it should go in a 
subfolder that is the name of mode.&lt;/p&gt;
&lt;h1 id=&quot;using-tabstops&quot;&gt;Using Tabstops&lt;/h1&gt;
&lt;p&gt;But why stop there? Let’s scaffold an entire Angular component and drop the cursor at 
locations where we need to make edits. Use &lt;code class=&quot;highlighter-rouge&quot;&gt;$&lt;/code&gt; followed by a number.  After expanding the 
snippet, the cursor is placed at &lt;code class=&quot;highlighter-rouge&quot;&gt;$1&lt;/code&gt;.  Pressing &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; takes the cursor to &lt;code class=&quot;highlighter-rouge&quot;&gt;$2&lt;/code&gt;.  &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; 
again and go to &lt;code class=&quot;highlighter-rouge&quot;&gt;$3&lt;/code&gt;, etc. &lt;code class=&quot;highlighter-rouge&quot;&gt;$0&lt;/code&gt; is the last stop, which also exits the snippet mode.&lt;/p&gt;

&lt;p&gt;Exapanding on the previous example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mode:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;snippet&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Angular&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;component&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;key:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;comp&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'@angular/core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;@Component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;selector:&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'app-$2',&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;styleUrls:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;'$3.component.scss'],&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1Component&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;{}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After we expand the snippet, in order as we &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; through:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Start by naming the exported class (line 13)&lt;/li&gt;
  &lt;li&gt;name the selector (line 8)&lt;/li&gt;
  &lt;li&gt;finish the stylesheet file name (line 9)&lt;/li&gt;
  &lt;li&gt;exit inside the config object where we’re likely to start the componet’s &lt;code class=&quot;highlighter-rouge&quot;&gt;template&lt;/code&gt; (line 10).&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;mirrors-and-text-transformations&quot;&gt;Mirrors and text transformations&lt;/h1&gt;
&lt;p&gt;If we follow certain naming convetions, we might end up typing the same thing several times in the above
example.  We can use mirrors and text transformations to save us the hassle.  A field like &lt;code class=&quot;highlighter-rouge&quot;&gt;$1&lt;/code&gt; can be repeated 
in the template where what we type at that tabstop will also be inserted at another locations.
We can also use Elisp to perform text transformations on the mirrors - like upcasing, capitalizing, etc.&lt;/p&gt;

&lt;p&gt;Here is the snippet that I used to make the Angular component in the 
&lt;a href=&quot;#crank-out-boiler-plate-code-like-a-boss-with-yasnippet&quot;&gt;animated gif&lt;/a&gt; at the start of this post.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mode:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;snippet&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-*-&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Angular&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;component&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;key:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;comp&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'@angular/core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;@Component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;selector:&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'app-$1',&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;styleUrls:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;'$1.component.scss'],&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;${1:$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;replace-regexp-in-string&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;capitalize&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;yas-text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;}Component&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;{}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now all we need to do is type the selector name at the first &lt;code class=&quot;highlighter-rouge&quot;&gt;$1&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;TAB&lt;/code&gt; out to the exit point.
The following now happens automatically:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the name is copied to the styleUrls&lt;/li&gt;
  &lt;li&gt;the name is copied to the class name. the class name is using a tabstop with a 
placeholder the syntax of which looks like this: &lt;code class=&quot;highlighter-rouge&quot;&gt;%{N:placeholder_value}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The class name, represented by &lt;code class=&quot;highlighter-rouge&quot;&gt;yas-text&lt;/code&gt; is transformed from kebab case to camel 
case using a bit of Elisp.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;YASnippet is great to cranking out boiler plates or code that is strucurally predictive by virture 
of following convetions. It’s highly customizable with tabstops, mirros and transformations.&lt;/p&gt;

&lt;p&gt;The is by no means at all exhaustive.  There’s a lot more you can do with YASnippet. Check out the
resouces below to learn more.&lt;/p&gt;

&lt;h1 id=&quot;resources&quot;&gt;Resources&lt;/h1&gt;

&lt;p&gt;Lean more with these awesome resources!&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=-4O-ZYjQxks&quot;&gt;Youtube YASnippet tutorial&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://joaotavora.github.io/yasnippet/&quot;&gt;YASnippet Documentation&lt;/a&gt; (Very helpful)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/joaotavora/yasnippet&quot;&gt;YASnippet Github repo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.emacswiki.org/emacs/Yasnippet&quot;&gt;YASnippet Emacs Wiki&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Bcompletion/auto-completion#yasnippet&quot;&gt;Yasnippet Spacemacs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://jr0cket.co.uk/2016/07/spacemacs-adding-your-own-yasnippets.html&quot;&gt;jr0cket’s awesome YASnippet tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">Crank Out Boiler Plate Code Like a Boss with YASnippet</summary></entry><entry><title type="html">Setting Default Headers in Angular</title><link href="https://blog.alex-miller.co/angular/2017/05/13/default-headers-in-angular.html" rel="alternate" type="text/html" title="Setting Default Headers in Angular" /><published>2017-05-13T05:00:00+00:00</published><updated>2017-05-13T05:00:00+00:00</updated><id>https://blog.alex-miller.co/angular/2017/05/13/default-headers-in-angular</id><content type="html" xml:base="https://blog.alex-miller.co/angular/2017/05/13/default-headers-in-angular.html">&lt;p&gt;When building an Angular app, the chances are high that you’re going to 
need to set the same headers for most, if not all, of your service 
class’ API requests. In this post, I’m going to demonstrate how to set
default headers both statically and dynamically. The examples below were done
using Angular 4.0.0.&lt;/p&gt;

&lt;h3 id=&quot;setting-request-headers-in-a-service-class&quot;&gt;Setting Request Headers in a Service Class&lt;/h3&gt;
&lt;p&gt;For setting headers in a one-off request, we can just define headers as
&lt;code class=&quot;highlighter-rouge&quot;&gt;RequestOptions&lt;/code&gt; and pass them as a argument to the &lt;code class=&quot;highlighter-rouge&quot;&gt;http&lt;/code&gt; client’s function. Keep in
mind that you’ll need to import &lt;code class=&quot;highlighter-rouge&quot;&gt;Headers&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;RequestOptions&lt;/code&gt; from &lt;code class=&quot;highlighter-rouge&quot;&gt;@angular/http&lt;/code&gt;
into the service class.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;p&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;Injectable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;WidgetService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;updateWidget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Observable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Widget&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
      &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;application/vnd.api+json&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;headers&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`widgets/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Having to do this for every request will get old real fast, not to metion leave you with 
repetitive code. Let’s see how to set the &lt;code class=&quot;highlighter-rouge&quot;&gt;Content-Type&lt;/code&gt; header for all requests by default.&lt;/p&gt;

&lt;h3 id=&quot;setting-static-default-request-headers&quot;&gt;Setting Static Default Request Headers&lt;/h3&gt;
&lt;p&gt;To ensure that every request from the &lt;code class=&quot;highlighter-rouge&quot;&gt;http&lt;/code&gt; client is configured with the same headers, we 
need to define a class to hold this configuration. Our class should be a subclass of 
&lt;code class=&quot;highlighter-rouge&quot;&gt;BaseRequestOptions&lt;/code&gt;.  Then, in the &lt;code class=&quot;highlighter-rouge&quot;&gt;constructor&lt;/code&gt;, we can set our headers.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;p&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;Injectable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptionsService&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;BaseRequestOptions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;application/vnd.api+json&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the &lt;code class=&quot;highlighter-rouge&quot;&gt;app.module&lt;/code&gt; we can tell Angular to use our custom class instead of 
the default &lt;code class=&quot;highlighter-rouge&quot;&gt;RequestOptions&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;nx&quot;&gt;providers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;useClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptionsService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Setting the headers in this way means that the default headers are set when the Angular
application starts up.  In many cases this is just fine, but what if you need to set
headers dynamically?  Let’s take a look at how that can be done.&lt;/p&gt;

&lt;h3 id=&quot;setting-dynamic-default-request-headers&quot;&gt;Setting Dynamic Default Request Headers&lt;/h3&gt;
&lt;p&gt;Let’s consider the following use case.  Our app needs to authenticate user requests using an
access token.  The user authenticates with their email and password and the API responds with
a JWT which our app stores in the browser’s &lt;code class=&quot;highlighter-rouge&quot;&gt;localStorage&lt;/code&gt;. Each subsequent request must have
the JWT in the &lt;code class=&quot;highlighter-rouge&quot;&gt;Authorization&lt;/code&gt; header.  The problem is that before we have added the JWT to
&lt;code class=&quot;highlighter-rouge&quot;&gt;localStorage&lt;/code&gt;, the headers have already been set.  Our &lt;code class=&quot;highlighter-rouge&quot;&gt;Authorization&lt;/code&gt; header might look like
this: &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;Authorization&quot;: &quot;Beaer &quot;&lt;/code&gt; - missing the JWT.  Another use case might involve setting a timestamp.&lt;/p&gt;

&lt;p&gt;The solution is to override the &lt;code class=&quot;highlighter-rouge&quot;&gt;merge&lt;/code&gt; function.  Every request merges options so we can define 
our dynamic headers there.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;p&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;Injectable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptionsService&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;BaseRequestOptions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;application/vnd.api+json&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptionsArgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;RequestOptions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newOptions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;newOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Authorization&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;s2&quot;&gt;`Beaer &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localStorage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;app-token&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://angular.io/docs/ts/latest/api/http/index/RequestOptions-class.html&quot;&gt;RequestOptions documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/classes.html&quot;&gt;Typescript Classes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">When building an Angular app, the chances are high that you’re going to need to set the same headers for most, if not all, of your service class’ API requests. In this post, I’m going to demonstrate how to set default headers both statically and dynamically. The examples below were done using Angular 4.0.0.</summary></entry><entry><title type="html">Concurrent Processes in Elixir</title><link href="https://blog.alex-miller.co/elixir/2017/04/19/concurrent-processes-in-elixir.html" rel="alternate" type="text/html" title="Concurrent Processes in Elixir" /><published>2017-04-19T19:00:00+00:00</published><updated>2017-04-19T19:00:00+00:00</updated><id>https://blog.alex-miller.co/elixir/2017/04/19/concurrent-processes-in-elixir</id><content type="html" xml:base="https://blog.alex-miller.co/elixir/2017/04/19/concurrent-processes-in-elixir.html">&lt;h3 id=&quot;an-elixir-noob-stumbles-into-concurrency-oriented-programming&quot;&gt;An Elixir Noob Stumbles into Concurrency-oriented Programming&lt;/h3&gt;

&lt;p&gt;Like many Rubyists today, I too am learning Elixir.  So far, it’s been an
exciting endeavor.  I started out by reading &lt;a href=&quot;https://www.manning.com/books/elixir-in-action&quot;&gt;&lt;strong&gt;Elixir in
Action&lt;/strong&gt;&lt;/a&gt; and was totally blown
away by the first chapter in which the author, Saša Jurić, teaches the reader
about Erlang concurrency.  Indeed, the Erlang VM (aka BEAM), is an amazing thing: it
supports it’s own processes (rather than OS processes) that execute code concurrently 
accross CPU cores. Whats more, the BEAM processes are super cheap; they don’t 
share memory; and, if long-running, schedulers will prevent them from 
halting the entire system by giving processes limited execution windows. 
When I finished that first chapter, I was hooked.&lt;/p&gt;

&lt;p&gt;Since then, I’ve been spending time learning Elixir as basically a weekend
project.  I read a couple more chapters of &lt;a href=&quot;https://www.manning.com/books/elixir-in-action&quot;&gt;&lt;strong&gt;Elixir in
Action&lt;/strong&gt;&lt;/a&gt; and familiarized
myself with the basic concepts of functional programming in Elixir.  I learned
about the power of pattern matching, recurision and how to work with immutable data.
While all of these concepts are great, what about the concurrent awesomeness 
of the BEAM?&lt;/p&gt;

&lt;p&gt;On March 2-3, 2017, I attended &lt;a href=&quot;http://www.elixirdaze.com/&quot;&gt;&lt;strong&gt;Elixir Daze&lt;/strong&gt;&lt;/a&gt;
where I heard Jesse Anderson’s &lt;a href=&quot;https://www.youtube.com/watch?v=8X0IWW2GJoQ&quot;&gt;&lt;strong&gt;The ABCs of OTP&lt;/strong&gt;&lt;/a&gt;.
His fantastic talk pointed me in the right direction.  Per his recommendation, I picked up a copy of
&lt;a href=&quot;https://www.manning.com/books/the-little-elixir-and-otp-guidebook&quot;&gt;&lt;strong&gt;The Little Elixir &amp;amp; OTP Guidebook&lt;/strong&gt;&lt;/a&gt;
and got to work.  Now, I’m on the good stuff: OTP.&lt;/p&gt;

&lt;h3 id=&quot;concurrent-processes-and-blasting-off-rockets&quot;&gt;Concurrent Processes and Blasting Off Rockets&lt;/h3&gt;

&lt;p&gt;If you have just started learning Elixir but want a quick and 
tiny taste of concurrent programming, here is something you can do in quickly 
in IEx. It super high level, absurdly trivial, tip-of-the-iceberg stuff using primitives, 
but it’s a just an introduction. Down below, I’ve provided some links to some real 
resources to learn more.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;What’s cooler than blasting off a rocket?  Blasting off two rockets at the same time. 
Create a file called &lt;code class=&quot;highlighter-rouge&quot;&gt;blast_off.ex&lt;/code&gt; and add the following code.&lt;/p&gt;

    &lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;BlastOff&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_ready&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;receive&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
       &lt;span class=&quot;ss&quot;&gt;:countdown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
         &lt;span class=&quot;no&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
           &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
           &lt;span class=&quot;ss&quot;&gt;:timer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Blast Off!&quot;&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;Here we define a module &lt;code class=&quot;highlighter-rouge&quot;&gt;BlastOff&lt;/code&gt; with one function called &lt;code class=&quot;highlighter-rouge&quot;&gt;get_ready&lt;/code&gt;. The interesting thing 
 here is the &lt;code class=&quot;highlighter-rouge&quot;&gt;receive/1&lt;/code&gt; function.  The processes running the &lt;code class=&quot;highlighter-rouge&quot;&gt;receive/1&lt;/code&gt; function will wait for
 a message that matches the pattern defined within the &lt;code class=&quot;highlighter-rouge&quot;&gt;do&lt;/code&gt; block.  In our &lt;code class=&quot;highlighter-rouge&quot;&gt;receive/1&lt;/code&gt; function
 we’re expecting a message that is the symbol, &lt;code class=&quot;highlighter-rouge&quot;&gt;:countdown&lt;/code&gt;.  Upon receipt of the &lt;code class=&quot;highlighter-rouge&quot;&gt;:countdown&lt;/code&gt; 
 message we start a countdown from 10 to 0 printing each number and sleeping for 1 second.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Now lets start up IEx session with our &lt;code class=&quot;highlighter-rouge&quot;&gt;BlastOff&lt;/code&gt; module loaded.&lt;/p&gt;

    &lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;iex&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blast_off&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Use &lt;code class=&quot;highlighter-rouge&quot;&gt;spawn/3&lt;/code&gt; to start up a process that runs our &lt;code class=&quot;highlighter-rouge&quot;&gt;BlastOff.get_ready/0&lt;/code&gt; function. It’s going to wait for
the &lt;code class=&quot;highlighter-rouge&quot;&gt;:countdown&lt;/code&gt; message. We bind the result of this function to &lt;code class=&quot;highlighter-rouge&quot;&gt;pid1&lt;/code&gt;.&lt;/p&gt;

    &lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;BlastOff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:get_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#PID&amp;lt;0.92.0&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;spawn/3&lt;/code&gt; function creates a process out of the given module, function and arguments.  In our case
the augment list is empty since our &lt;code class=&quot;highlighter-rouge&quot;&gt;get_ready&lt;/code&gt; function doesn’t take any arguments.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Lets spawn another process and bind the result to &lt;code class=&quot;highlighter-rouge&quot;&gt;pid2&lt;/code&gt;.&lt;/p&gt;

    &lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;BlastOff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:get_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#PID&amp;lt;0.94.0&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Fire up the &lt;a href=&quot;http://erlang.org/doc/apps/observer/observer_ug.html&quot;&gt;Erlang Observer&lt;/a&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;iex&amp;gt; :observer.start&lt;/code&gt;.
We should see our processes.&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://monosnap.com/file/paqffXcUKalvKIubFXLCbfRDHVyI1W.png&quot; alt=&quot;observer&quot; height=&quot;500px&quot; width=&quot;700px&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;Notice now little memory these processes are consuming (those numbers are in bytes).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Now, let’s launch a couple rockets. Send the &lt;code class=&quot;highlighter-rouge&quot;&gt;:countdown&lt;/code&gt; message to our processes and observe the results.
In the example below, we’re iterating over the list of pids we created earlier then 
using &lt;code class=&quot;highlighter-rouge&quot;&gt;send/2&lt;/code&gt; to send each pid the &lt;code class=&quot;highlighter-rouge&quot;&gt;:countdown&lt;/code&gt; message.&lt;/p&gt;

    &lt;div class=&quot;language-elixir highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:countdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;You should now see the results of both processes counting down at the same time.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This example is painfully trivial, but it serves as a very simple introduction into concurrent
processes in Elixir.  Going beyond this, means diving into GenServer and OTP: the really good stuff that makes 
Elixir truly awesome and powerful. Here are some great resources to learn more.&lt;/p&gt;

&lt;h3 id=&quot;resources-for-further-learning&quot;&gt;Resources for Further Learning&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.manning.com/books/the-little-elixir-and-otp-guidebook&quot;&gt;&lt;strong&gt;The Little Elixir &amp;amp; OTP Guidebook&lt;/strong&gt;&lt;/a&gt;:
Do you want learn OTP now?  This do yourself a favor and get this book.  I highly recommend it.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8X0IWW2GJoQ&quot;&gt;&lt;strong&gt;The ABCs of OTP&lt;/strong&gt;&lt;/a&gt;: Jesse Anderson’s Elixir talk.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/jessejanderson/16cbc0614e9194fa1b64460f775777ab&quot;&gt;Jessie Anderson’s &lt;strong&gt;ABCs of OTP&lt;/strong&gt; resource guide&lt;/a&gt;: This is really cool - he lists some great resouces for learning OTP.  I found this to be an excellent guide.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.elixirdaze.com/&quot;&gt;&lt;strong&gt;Elixir Daze&lt;/strong&gt;&lt;/a&gt;: A super cool Elixir Conference in Saint Auguestine, FL.&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">An Elixir Noob Stumbles into Concurrency-oriented Programming</summary></entry><entry><title type="html">Rails Authenticity Token and Mobile Safari</title><link href="https://blog.alex-miller.co/rails/2017/01/07/rails-authenticity-token-and-mobile-safari.html" rel="alternate" type="text/html" title="Rails Authenticity Token and Mobile Safari" /><published>2017-01-07T23:26:00+00:00</published><updated>2017-01-07T23:26:00+00:00</updated><id>https://blog.alex-miller.co/rails/2017/01/07/rails-authenticity-token-and-mobile-safari</id><content type="html" xml:base="https://blog.alex-miller.co/rails/2017/01/07/rails-authenticity-token-and-mobile-safari.html">&lt;p&gt;At &lt;a href=&quot;http://trimagency.com/&quot;&gt;Trim Agency&lt;/a&gt;, we typically architect our web apps by building a Ruby on Rails API separate from an Angular single page font-end app.  Recently, however, we started work on a brown field project that was fully Rails, front-end as well.  After deploying and getting close to one hundred users, I was surprised to see an influx of &lt;code class=&quot;highlighter-rouge&quot;&gt;ActionController::InvalidAuthenticityToken&lt;/code&gt; exceptions.  I didn’t take long to see the common thread between these exceptions:  ‘iPhone’ or ‘Mobile Safari’ was consistently appearing in the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent&quot;&gt;User-Agent header&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I discovered this problem is fairly common, after I read this &lt;a href=&quot;https://github.com/rails/rails/issues/21948&quot;&gt;Rails Github issue&lt;/a&gt;. Here I will summarize the problem and demonstrate the solution that worked for us.&lt;/p&gt;

&lt;h3 id=&quot;what-is-the--authenticity-token&quot;&gt;What is the  &lt;em&gt;Authenticity Token&lt;/em&gt;?&lt;/h3&gt;

&lt;p&gt;It’s a counter measure to &lt;a href=&quot;https://en.wikipedia.org/wiki/Cross-site_request_forgery&quot;&gt;cross-site request forgery (CSRF)&lt;/a&gt;.  Rails will include the &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; in all forms in a hidden input field. For AJAX requests the token is placed in a header called &lt;code class=&quot;highlighter-rouge&quot;&gt;X-CSRF-Token&lt;/code&gt;.  The &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; is verified on the server for all non-GET requests in order to ensure that the request came from the user and not someone else.  How does it do that?&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; is also stored in session (&lt;code class=&quot;highlighter-rouge&quot;&gt;session[:_csrf_token]&lt;/code&gt;) which is encrypted in the client’s cookie.  Since the client’s cookie is automatically sent to the server with a request, Rails can compare the &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; in the client’s cookie with the &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; submitted in the form. If the tokens match, the request can proceed.  If they don’t match…well, let’s see what can happen.&lt;/p&gt;

&lt;h3 id=&quot;request-forgery-protection&quot;&gt;Request Forgery Protection&lt;/h3&gt;
&lt;p&gt;For non-APIs, rails will, by default, add the following to the ApplicationController:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ApplicationController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActionController&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;protect_from_forgery&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;with: :exception&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So, when the tokens do not match, Rails will raise the &lt;code class=&quot;highlighter-rouge&quot;&gt;ActionController::InvalidAuthenticityToken&lt;/code&gt; exception which will stop the request.  How you decide to handle it, is up to you.  As you will see, legitimate users can get this error, so you’ll want to handle it somehow.  For example, you might redirect the user to an error page with a 422 response.&lt;/p&gt;

&lt;h3 id=&quot;whats-going-on-with-mobile-safari&quot;&gt;What’s Going On With Mobile Safari?&lt;/h3&gt;

&lt;p&gt;I was able to reproduce the error easily on mobile Safari on iPhone using the following steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Log into the app&lt;/li&gt;
  &lt;li&gt;Sign out of the app so I will be redirect back to the log in screen&lt;/li&gt;
  &lt;li&gt;Close the browser (force close it using the home double tap and swipe method)&lt;/li&gt;
  &lt;li&gt;Reopen Safari and try to log in.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After submitting my credentials, the &lt;code class=&quot;highlighter-rouge&quot;&gt;ActionController::InvalidAuthenticityToken&lt;/code&gt; was raised.  Here is why:&lt;/p&gt;

&lt;p&gt;Rails uses a session cookie as opposed to a persistent cookie to store the &lt;em&gt;authenticity token&lt;/em&gt;.  By design, session cookies are deleted when browsers are closed.  Upon re-opening mobile Safari, the browser tab is reloaded entirely from cache and the session cookie is gone.  Thus, upon submitting the login form the &lt;code class=&quot;highlighter-rouge&quot;&gt;authenticity_token&lt;/code&gt; in the form cannot be verified.  This didn’t seem to be an issue with other browsers because they appear to retain the session cookies to let users continue where they left off without issue.&lt;/p&gt;

&lt;h3 id=&quot;the-solution&quot;&gt;The Solution&lt;/h3&gt;

&lt;p&gt;Tell Safari (and all browsers) to request the page before submitting the again.  This can be done via the &lt;code class=&quot;highlighter-rouge&quot;&gt;Cache-Control&lt;/code&gt; header and these two directives: &lt;code class=&quot;highlighter-rouge&quot;&gt;no-cache&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;no-store&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;no-store&lt;/code&gt;: a response is not allowed to be cached by the browser. It must be fetched on every request.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;no-cache&lt;/code&gt;: caches need to submit the request for validation first to check if the resource has changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# config/application.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Application&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;action_dispatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;default_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;merge!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;s1&quot;&gt;'Cache-Control'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'no-store, no-cache'&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Actually, I don’t know why both are needed.  It seems to me that &lt;code class=&quot;highlighter-rouge&quot;&gt;no-store&lt;/code&gt; should be enough.&lt;/p&gt;

&lt;p&gt;After deploying the above change and monitoring the app for a week, I haven’t seen a single &lt;code class=&quot;highlighter-rouge&quot;&gt;ActionController::InvalidAuthenticityToken&lt;/code&gt; exception.  For more information on the topics covered in this post, see resources below.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://guides.rubyonrails.org/security.html#csrf-countermeasures&quot;&gt;Ruby on Rails security guide&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html&quot;&gt;Rails forgery protection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control&quot;&gt;Cache-Control header spec&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en#cache-control&quot;&gt;HTTP Caching tutorial by Ilya Grigorik&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">At Trim Agency, we typically architect our web apps by building a Ruby on Rails API separate from an Angular single page font-end app. Recently, however, we started work on a brown field project that was fully Rails, front-end as well. After deploying and getting close to one hundred users, I was surprised to see an influx of ActionController::InvalidAuthenticityToken exceptions. I didn’t take long to see the common thread between these exceptions: ‘iPhone’ or ‘Mobile Safari’ was consistently appearing in the User-Agent header.</summary></entry><entry><title type="html">What Can Daru Do For You?</title><link href="https://blog.alex-miller.co/ruby/2016/12/24/what-can-daru-do-for-you.html" rel="alternate" type="text/html" title="What Can Daru Do For You?" /><published>2016-12-24T02:38:35+00:00</published><updated>2016-12-24T02:38:35+00:00</updated><id>https://blog.alex-miller.co/ruby/2016/12/24/what-can-daru-do-for-you</id><content type="html" xml:base="https://blog.alex-miller.co/ruby/2016/12/24/what-can-daru-do-for-you.html">&lt;p&gt;Data Analysis in RUby&lt;/p&gt;

&lt;p&gt;At my job, I started work on a client project that was rather number intensive.  We were going to have to perform repetitive calculations over a dataset.  A colleague introduced me to the concept of data frames and the Ruby gem Daru.&lt;/p&gt;

&lt;h3 id=&quot;what-is-daru&quot;&gt;What is Daru?&lt;/h3&gt;

&lt;p&gt;It’s a data analysis tool that lets us build tabular data sets which we can then manipulate and apply linear calculations.  It’s also is a data visualization tool.  It’s worth noting that Daru is quite similar in functionality to the &lt;a href=&quot;http://pandas.pydata.org/&quot;&gt;pandas Python library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can do alot with Daru, but I’ll just be looking at a very small piece of it here.  See &lt;a href=&quot;#resources&quot;&gt;Resources&lt;/a&gt; section below for a link to the docs.&lt;/p&gt;

&lt;h3 id=&quot;vectors-and-dataframes&quot;&gt;Vectors and DataFrames&lt;/h3&gt;

&lt;h4 id=&quot;vector&quot;&gt;Vector&lt;/h4&gt;
&lt;p&gt;A &lt;em&gt;Vector&lt;/em&gt; is a one dimensional set of data, like an array.  When I was working with Daru, I liked to think of a &lt;em&gt;Vector&lt;/em&gt; as a single column from a spreadsheet. In this example, I am mocking 7 day price history of some imaginary product. Note that the &lt;em&gt;Vector&lt;/em&gt; can be named.  Also, note the numeric index.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'daru'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;prices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;499&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;599&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Daru&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;name: :price_cents&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::Vector(7)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#            price_cents&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          0         505&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          1         544&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          2         552&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          3         501&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          4         534&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          5         516&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#          6         541&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;dataframe&quot;&gt;DataFrame&lt;/h4&gt;
&lt;p&gt;A &lt;em&gt;DataFrame&lt;/em&gt; on the other hand is two dimensional, like a spreadsheet.  Expanding
on the example above, we can include a date column.  We instantiate the
&lt;em&gt;DataFrame&lt;/em&gt; with a hash of arrays.  Each key of the hash is a column name and
the array contains the data.  This is one of several ways to compose a
&lt;em&gt;DataFrame&lt;/em&gt;.  Another way is to use &lt;em&gt;Vectors&lt;/em&gt; instead of arrays - &lt;em&gt;Vector&lt;/em&gt;
indicies are lined up with each other.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'daru'&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'date'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;prices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;499&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;599&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dates&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2016&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Daru&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;date: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;price_cents: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prices&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;name: :product_prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;order: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:price_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::DataFrame: product_prices (7x2)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#                 date price_cent&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0 2016-01-01        501&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         1 2016-01-02        591&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         2 2016-01-03        543&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         3 2016-01-04        558&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         4 2016-01-05        528&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         5 2016-01-06        552&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         6 2016-01-07        558&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;so-i-have-a-dataframenow-what&quot;&gt;So I have a &lt;em&gt;DataFrame&lt;/em&gt;…Now what?&lt;/h3&gt;
&lt;p&gt;We can perform analysis on the data frame like finding the mean, counts, min,
and max.  Also we can find covariance and correlation bewtween &lt;em&gt;Vectors&lt;/em&gt;. It’s
also possible to perform SQL like queries against the data. There is also
filtering and sorting…the list goes on and on.  See the &lt;a href=&quot;http://www.rubydoc.info/gems/daru/0.1.4.1&quot;&gt;documentation&lt;/a&gt; for the details.  Here, I’ll show a couple of examples that I found
useful.&lt;/p&gt;

&lt;h4 id=&quot;add-a-rolling-mean-column&quot;&gt;Add a rolling mean column&lt;/h4&gt;
&lt;p&gt;Here is an example of adding a column to the &lt;em&gt;DataFrame&lt;/em&gt; that is a rolling mean
calculation on the price column with a lookback of 7.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Daru&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;date: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;price_cents: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prices&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;name: :product_prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;order: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:price_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Add a column that is a rolling mean of price with a lookback of 7.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:r_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:price_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rolling_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::DataFrame: product_prices (14x3)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#                 date price_cent     r_mean&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0 2016-01-01        573        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         1 2016-01-02        587        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         2 2016-01-03        511        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         3 2016-01-04        501        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         4 2016-01-05        548        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         5 2016-01-06        562        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         6 2016-01-07        567 549.857142&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         7 2016-01-08        503 539.857142&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         8 2016-01-09        503 527.857142&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         9 2016-01-10        565 535.571428&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        10 2016-01-11        580 546.857142&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        11 2016-01-12        567 549.571428&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        12 2016-01-13        540 546.428571&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        13 2016-01-14        514 538.857142&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;do-some-arithmetic&quot;&gt;Do some arithmetic.&lt;/h4&gt;
&lt;p&gt;What if we want to calculate the price difference with a lookback of 7.  Here is
one way we could solve it with the &lt;code class=&quot;highlighter-rouge&quot;&gt;lag&lt;/code&gt; method.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# First we could add a column that is the price_cents column shifted by 7. &lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:prev_week_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:price_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::DataFrame: product_prices (14x3)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#                 date price_cent prev_week_&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0 2016-01-01        515        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         1 2016-01-02        567        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         2 2016-01-03        528        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         3 2016-01-04        593        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         4 2016-01-05        599        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         5 2016-01-06        563        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         6 2016-01-07        520        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         7 2016-01-08        579        515&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         8 2016-01-09        551        567&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         9 2016-01-10        575        528&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        10 2016-01-11        541        593&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        11 2016-01-12        529        599&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        12 2016-01-13        594        563&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        13 2016-01-14        573        520&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Now we can calculate the difference between values for each index.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:weekly_change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:price_cents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:prev_week_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::DataFrame: product_prices (14x4)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#                 date price_cent prev_week_ weekly_cha&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0 2016-01-01        590        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         1 2016-01-02        515        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         2 2016-01-03        507        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         3 2016-01-04        591        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         4 2016-01-05        556        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         5 2016-01-06        534        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         6 2016-01-07        524        nil        nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         7 2016-01-08        562        590        -28&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         8 2016-01-09        544        515         29&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         9 2016-01-10        553        507         46&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        10 2016-01-11        577        591        -14&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        11 2016-01-12        530        556        -26&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        12 2016-01-13        502        534        -32&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        13 2016-01-14        511        524        -13&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;join-dataframes&quot;&gt;Join &lt;em&gt;DataFrames&lt;/em&gt;&lt;/h4&gt;
&lt;p&gt;Let’s say we want to compare the prices of two products.  We could join two
&lt;em&gt;DateFrames&lt;/em&gt; together à la SQL.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Product 'a' prices&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Daru&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;date: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;price_a: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prices_a&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;name: :product_a_prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Product 'b' prices&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Daru&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;date: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;ss&quot;&gt;price_b: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prices_b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;name: :product_b_prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# We can join both DataFrames on the :date Vector&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df_joins&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;on: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;how: :left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#&amp;lt;Daru::DataFrame(14x3)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#              price_a       date    price_b&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0        541 2016-01-01        566&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         1        570 2016-01-02        578&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         2        561 2016-01-03        541&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         3        537 2016-01-04        535&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         4        594 2016-01-05        570&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         5        541 2016-01-06        586&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         6        536 2016-01-07        539&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         7        586 2016-01-08        586&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         8        596 2016-01-09        510&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         9        572 2016-01-10        565&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        10        536 2016-01-11        535&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        11        536 2016-01-12        574&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        12        549 2016-01-13        505&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#        13        597 2016-01-14        538&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Daru is a powerful data analysis tool which I have barely scratched the surface. 
Some other things of note:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Creating &lt;em&gt;DataFrames&lt;/em&gt; from CSVs or Excel files&lt;/li&gt;
  &lt;li&gt;Grouping and aggregating data&lt;/li&gt;
  &lt;li&gt;Graceful handling of missing data (nils)&lt;/li&gt;
  &lt;li&gt;Pivot tables&lt;/li&gt;
  &lt;li&gt;Data visulization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is much more to this library that what I have shown, so I encourage the
reader to explore more.  I have provided some links below to get started.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.rubydoc.info/gems/daru/0.1.4.1&quot;&gt;Daru documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/v0dro/daru&quot;&gt;Daru Github repo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/v0dro/daru/wiki/pandas-vs-daru&quot;&gt;Daru vs panda&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Alex Miller</name></author><summary type="html">Data Analysis in RUby</summary></entry></feed>