Mathias Jean JohansenMy personal website and blog.https://mjj.io
Common Pitfalls in Ruby<p>Last week, I stumbled upon a document covering a list of common pitfalls in Ruby
that I assembled in the beginning of 2015. Browsing through the various gotchas,
I decided to rework the list, and publish it here for future reference.
Hopefully, others will find it useful too.</p>
<h2 id="and-or-and-not"><code class="highlighter-rouge">and</code>, <code class="highlighter-rouge">or</code>, and <code class="highlighter-rouge">not</code></h2>
<p>Since Ruby is often considered a readable language, many novices tend to believe
that <code class="highlighter-rouge">and</code>, <code class="highlighter-rouge">or</code>, and <code class="highlighter-rouge">not</code> are more appealing alternatives to <code class="highlighter-rouge">&amp;&amp;</code>, <code class="highlighter-rouge">||</code>, and
<code class="highlighter-rouge">!</code>, respectively. They differ, however, in their behavior. Consider the
following example:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">foo</span> <span class="o">=</span> <span class="kp">true</span> <span class="n">and</span> <span class="kp">false</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="n">foo</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="n">foo</span> <span class="o">=</span> <span class="kp">true</span> <span class="o">&amp;&amp;</span> <span class="kp">false</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="n">foo</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="n">foo</span> <span class="o">=</span> <span class="kp">false</span> <span class="o">||</span> <span class="kp">true</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="n">foo</span> <span class="o">=</span> <span class="kp">false</span> <span class="n">or</span> <span class="kp">true</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="n">foo</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
</code></pre></div></div>
<p>Similarly, this example below reveals the issue with <code class="highlighter-rouge">not</code> versus <code class="highlighter-rouge">!</code>.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">not</span> <span class="kp">true</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="o">!</span><span class="kp">true</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="n">not</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">4</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="o">!</span><span class="mi">3</span> <span class="o">==</span> <span class="mi">4</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
</code></pre></div></div>
<p>In the first example, <code class="highlighter-rouge">foo = true and false</code> will be interpreted as <code class="highlighter-rouge">(foo =
true) and false</code> where <code class="highlighter-rouge">foo = true &amp;&amp; false</code> will be interpreted as <code class="highlighter-rouge">foo = (true
&amp;&amp; false)</code>, as <code class="highlighter-rouge">and</code> and <code class="highlighter-rouge">or</code> have lower precedence than <code class="highlighter-rouge">&amp;&amp;</code> and <code class="highlighter-rouge">||</code>. Thus,
<code class="highlighter-rouge">and</code> and <code class="highlighter-rouge">or</code> are meant for flow control, while <code class="highlighter-rouge">&amp;&amp;</code> and <code class="highlighter-rouge">||</code> are for boolean
logic.</p>
<p>Correspondingly, <code class="highlighter-rouge">not</code> and <code class="highlighter-rouge">!</code> also behave differently due to their precedence.
As expected, <code class="highlighter-rouge">not</code> has lower precedence than <code class="highlighter-rouge">!</code>.</p>
<p>As shown in the second example, <code class="highlighter-rouge">not 3 == 4</code> will be interpreted as <code class="highlighter-rouge">not (3 ==
4)</code> which evaluates to true, while <code class="highlighter-rouge">!3 == 4</code> will be interpreted as <code class="highlighter-rouge">(!3) == 4</code>,
i.e. <code class="highlighter-rouge">false == 4</code>.</p>
<h2 id="equal-eql--and-"><code class="highlighter-rouge">equal?</code>, <code class="highlighter-rouge">eql?</code>, <code class="highlighter-rouge">===</code>, and <code class="highlighter-rouge">==</code></h2>
<p>These four methods of determining equality all behave widely different. The
first method, <code class="highlighter-rouge">equal?</code>, is an identity comparison, so it will only return true
in situations where <code class="highlighter-rouge">a</code> is the same object as <code class="highlighter-rouge">b</code>. One can think of <code class="highlighter-rouge">equal?</code> as
a pointer comparison.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="mi">1</span><span class="p">.</span><span class="nf">equal?</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="s2">"a"</span><span class="p">.</span><span class="nf">equal?</span><span class="p">(</span><span class="s2">"a"</span><span class="p">)</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
</code></pre></div></div>
<p>It is worth nothing that if we enable immutable strings in the example above
with <code class="highlighter-rouge"># frozen_string_literal: true</code>, or the <code class="highlighter-rouge">--enable-frozen-string-literal </code>
flag, the last example will also evaluate to true.</p>
<p><code class="highlighter-rouge">eql?</code> is essentially for hash comparisons. It returns true when <code class="highlighter-rouge">a</code>, and <code class="highlighter-rouge">b</code>
refer to the same hash key. <code class="highlighter-rouge">Hash</code> uses this to test for equality. It is common
to alias <code class="highlighter-rouge">eql?</code> to <code class="highlighter-rouge">==</code>.</p>
<p>Finally, <code class="highlighter-rouge">===</code>, and <code class="highlighter-rouge">==</code> are for case equality, and generic equality,
respectively. <code class="highlighter-rouge">===</code> is typically overridden to provide meaningful semantics in
<code class="highlighter-rouge">case</code> statements for <code class="highlighter-rouge">Range</code>, <code class="highlighter-rouge">Regex</code>, and <code class="highlighter-rouge">Proc</code>. <code class="highlighter-rouge">==</code> is the most common
comparison operator, and therefore this is usually overridden to provide
class-specific meaning.</p>
<h2 id="to_s-to_str-and-string"><code class="highlighter-rouge">to_s</code>, <code class="highlighter-rouge">to_str</code>, and <code class="highlighter-rouge">String</code></h2>
<p><code class="highlighter-rouge">to_s</code>, and <code class="highlighter-rouge">String</code> are more or less equivalent. <code class="highlighter-rouge">String</code> will check the class
of its parameter, and if it is not already a string, it will call <code class="highlighter-rouge">to_s</code> on it.
Calling <code class="highlighter-rouge">to_s</code> obviously means it will be called regardless.</p>
<p><code class="highlighter-rouge">to_str</code> is different from the two, however. It should only be implemented in
situations where your object acts like a string as opposed to being
representable by a string meaning you should only implement <code class="highlighter-rouge">to_str</code> in your
classes for objects that are interchangeable with <code class="highlighter-rouge">String</code> objects.</p>
<h2 id="any"><code class="highlighter-rouge">any?</code></h2>
<p>The <code class="highlighter-rouge">Enumerable</code> module in Ruby defines an <code class="highlighter-rouge">any?</code> method. When I initially
learned Ruby, I expected that it would return true if the collection was
non-empty (as a negated <code class="highlighter-rouge">empty?</code>). Nevertheless, <code class="highlighter-rouge">any?</code> (without a provided
block) returns true if at least one of the collection members is not <code class="highlighter-rouge">false</code> or
<code class="highlighter-rouge">nil</code>. The following example demonstrates this behavior:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="kp">false</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">any?</span>
<span class="o">=&gt;</span> <span class="kp">false</span>
<span class="p">[</span><span class="kp">true</span><span class="p">,</span> <span class="kp">false</span><span class="p">].</span><span class="nf">any?</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="p">[</span><span class="ss">:truthy</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">any?</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
</code></pre></div></div>
<h2 id="super-and-super"><code class="highlighter-rouge">super</code>, and <code class="highlighter-rouge">super()</code></h2>
<p>In Ruby, we learn that we can omit parentheses in method calls without any
arguments, as <code class="highlighter-rouge">foo</code>, and <code class="highlighter-rouge">foo()</code> returns the same result, and abandoning
unnecessary parentheses is normally what most style guides advocate for.</p>
<p>Consequently, it might be rather tempting to leave out parentheses when calling
<code class="highlighter-rouge">super()</code> but calling <code class="highlighter-rouge">super</code>, and <code class="highlighter-rouge">super()</code> is not entirely the same in Ruby.
<code class="highlighter-rouge">super</code> (without parentheses) will call the parent method with exactly the same
arguments that were passed to the original method, while the latter will call
the parent method without any arguments at all.</p>
<h2 id="size-count-and-length"><code class="highlighter-rouge">size</code>, <code class="highlighter-rouge">count</code>, and <code class="highlighter-rouge">length</code></h2>
<p>Similar to other methods in this blog post, people may be tempted to think
that <code class="highlighter-rouge">size</code>, <code class="highlighter-rouge">count</code>, and <code class="highlighter-rouge">length</code> are simply aliases for the same operation
but this is yet another quirk of Ruby.</p>
<p><code class="highlighter-rouge">length</code>, and <code class="highlighter-rouge">size</code> are identical, and they usually run in constant time, so
they are faster than <code class="highlighter-rouge">count</code>. Unlike <code class="highlighter-rouge">count</code>, they are not a part of
<code class="highlighter-rouge">Enumerable</code> but rather a part of a concrete class (such as <code class="highlighter-rouge">Array</code>, or
<code class="highlighter-rouge">String</code>). Normally, I tend to use <code class="highlighter-rouge">length</code> for strings, and <code class="highlighter-rouge">size</code> for
collections.</p>
<p>As mentioned, <code class="highlighter-rouge">count</code> is a part of <code class="highlighter-rouge">Enumerable</code>, and it is usually meant to be
used with a block, although this is not mandatory.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">].</span><span class="nf">count</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:even?</span><span class="p">)</span>
<span class="o">=&gt;</span> <span class="mi">3</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">].</span><span class="nf">count</span>
<span class="o">=&gt;</span> <span class="mi">6</span>
</code></pre></div></div>
<h2 id="hashnew-vs-hashnew-h-k-hk---"><code class="highlighter-rouge">Hash.new([])</code> vs. <code class="highlighter-rouge">Hash.new {|h, k| h[k] = [] }</code></h2>
<p><code class="highlighter-rouge">Hash.new([])</code>, and <code class="highlighter-rouge">Hash.new {|h,k| h[k] = [] }</code> may look similar but they
behave slightly different. When accessing an unknown element, <code class="highlighter-rouge">Hash.new([])</code>
will always return the same array where <code class="highlighter-rouge">Hash.new {|h, k| h[k] = [] }</code> creates a
new array. A quick benchmark reveals that accessing an unknown element from a
hash initialized with <code class="highlighter-rouge">Hash.new([])</code> is approximately twice as fast as
accessing an unknown element from a hash initialized with <code class="highlighter-rouge">Hash.new {|h,k| h[k]
= [] }</code></p>
<p>This behavior can also be seen in arrays where <code class="highlighter-rouge">Array.new(42) { Foo.new }</code> will
initialize a new <code class="highlighter-rouge">Foo</code> every time, while <code class="highlighter-rouge">Array.new(42, Foo.new)</code> will refer to
the same <code class="highlighter-rouge">Foo</code> object for each element.</p>
<h2 id="the-flip-flop-operator-">The flip-flop operator (<code class="highlighter-rouge">..</code>)</h2>
<p>In Ruby, <code class="highlighter-rouge">..</code>, and <code class="highlighter-rouge">...</code> are most often used for ranges. It allows us to
succinctly express ranges from A to Z as such <code class="highlighter-rouge">'a'..'z'</code>. The <code class="highlighter-rouge">..</code> operator
always includes the last element where <code class="highlighter-rouge">...</code> will skip the last element in the
range.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="s1">'a'</span><span class="o">..</span><span class="s1">'z'</span><span class="p">).</span><span class="nf">to_a</span><span class="p">.</span><span class="nf">size</span>
<span class="o">=&gt;</span> <span class="mi">26</span>
<span class="p">(</span><span class="s1">'a'</span><span class="o">...</span><span class="s1">'z'</span><span class="p">).</span><span class="nf">to_a</span><span class="p">.</span><span class="nf">size</span>
<span class="o">=&gt;</span> <span class="mi">25</span>
</code></pre></div></div>
<p>We can also conveniently express a date range as such:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'date'</span>
<span class="o">=&gt;</span> <span class="kp">true</span>
<span class="n">now</span> <span class="o">=</span> <span class="no">DateTime</span><span class="p">.</span><span class="nf">now</span>
<span class="o">=&gt;</span> <span class="c1">#&lt;DateTime: 2016-12-15T22:36:38+01:00 ((2457738j,77798s,446146000n), +3600s, 2299161j)&gt;</span>
<span class="n">last_month</span> <span class="o">=</span> <span class="n">now</span> <span class="o">-</span> <span class="mi">30</span>
<span class="o">=&gt;</span> <span class="c1">#&lt;DateTime: 2016-11-15T22:36:38+01:00 ((2457708j,77798s,446146000n),+3600s,2299161j)&gt;</span>
<span class="p">(</span><span class="n">last_month</span><span class="o">..</span><span class="n">now</span><span class="p">).</span><span class="nf">to_a</span><span class="p">.</span><span class="nf">size</span>
<span class="o">=&gt;</span> <span class="mi">31</span>
</code></pre></div></div>
<p>The <code class="highlighter-rouge">..</code> operator can, however, lead to a bit of confusion since it has a different
behavior in other situations.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">20</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
<span class="nb">puts</span> <span class="n">x</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="o">..</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">end</span>
</code></pre></div></div>
<p>The condition in the loop above evaluates to false every time it is evaluated
until the first part, i.e. <code class="highlighter-rouge">x == 5</code>, evaluates to true. Then it evaluates to
true until the second part evaluates to true. In the above example, the
flip-flop is turned on when <code class="highlighter-rouge">x == 5</code> and stays on until <code class="highlighter-rouge">x == 10</code>, so the
numbers from 5 to 10 are printed.</p>
<p>The flip-flop operator only works inside <code class="highlighter-rouge">if</code>s and ternary conditions.
Everywhere else, Ruby considers it to be the range operator. With the flip-flop
operator, we now conclude our whirlwind tour of the various pitfalls in the Ruby
programming language.</p>
<p>As demonstrated in this blog post, Ruby has quite a few quirks. In order to
avoid many of these pitfalls, I usually advocate for using
<a href="https://github.com/bbatsov/rubocop">Rubocop</a> either locally on your own
machine, or ideally as a part of the CI pipeline. While it may not detect all
the issues at hand, it is at most times extremely good at reporting problems in
your code.</p>
Mon, 02 Jan 2017 00:00:00 +0000https://mjj.io/2017/01/02/common-pitfalls-in-ruby/
https://mjj.io/2017/01/02/common-pitfalls-in-ruby/Curry On: A Retrospective<p>This year’s <a href="http://curry-on.org/2016/">Curry On</a> conference in Rome ended last
night, and it was certainly worth the trip. While I saw a fair amount of
presentations throughout the conference, I’d like to briefly touch upon a few of
my favorite ones in this blog post.</p>
<p><a href="http://curry-on.org/2016/sessions/the-racket-manifesto.html">The Racket
Manifesto</a> by
<a href="http://www.ccs.neu.edu/home/matthias/">Matthias Felleisen</a> was a good reminder
that I need to spend more time with <a href="http://racket-lang.org/">Racket</a>. It is a
deeply flexible language, and it is clear that countless hours have been spent
refining it. Both <a href="http://www0.cs.ucl.ac.uk/staff/p.ohearn/">Peter O’Hearn’s</a>
talk <a href="http://curry-on.org/2016/sessions/move-fast-to-fix-more-things.html">Move Fast to Fix More
Things</a>,
and <a href="https://twitter.com/littlecalculist">Dave Herman</a>’s
<a href="http://curry-on.org/2016/sessions/building-an-open-source-research-lab.html">talk</a>
thoroughly examined their transitions from academia to industry, and how
Facebook, and Mozilla have benefited from research with
<a href="http://fbinfer.com/">Infer</a>, and <a href="https://www.rust-lang.org/en-US/">Rust</a>,
respectively. It is projects like these that demonstrate how cooperation between
the industry, and academia can truly lead to advancements in our field.</p>
<p>I also found <a href="https://twitter.com/cristalopes">Crista Lopes</a>’s presentation on
<a href="http://curry-on.org/2016/sessions/exercises-in-programming-style.html">Exercises in Programming
Style</a>
extremely entertaining. In the presentation, she essentially went through a
large range of different programming styles from imperative to
continuation-passing style, and it seemed somewhat reminiscent of <a href="http://www.willamette.edu/~fruehr/haskell/evolution.html">The Evolution
of a Haskell
Programmer</a> by Fritz
Ruehr from Willamette University which was also a joy to read.</p>
<p><a href="http://curry-on.org/2016/sessions/rascal-the-swiss-army-knife-of-metaprogramming.html">Rascal: the Swiss Army Knife of Meta
Programming</a>
by <a href="https://twitter.com/tvdstorm">Tijs van der Storm</a> was an interesting
presentation too. Although, I found the syntax to be a bit peculiar, it was
obvious that <a href="http://www.rascal-mpl.org/">Rascal</a> truly enables non-trivial
metaprogramming tasks to be solved almost effortlessly. The source-to-source
Java transformation in the presentation impressed me, and certainly made me
interested in the language. Finally, <a href="https://labs.oracle.com/pls/apex/f?p=labs:bio:0:21">Cristina
Cifuentes</a>’s presentation
<a href="http://curry-on.org/2016/sessions/are-we-ready-for-secure-languages.html">Are We Ready for Secure
Languages?</a>
greatly illustrated how we should start thinking security into our languages
rather than adding security measures as an afterthought.</p>
<p>In addition to the numerous marvelous talks, I also found the conference to be
extremely sympathetic. It was quite clear that the organizers had put an effort
into encouraging diversity which I greatly appreciated, and I think the
conference succeeded tremendously in enabling a conversation between academia,
and the industry. Finally, it did not hurt that this was the venue:</p>
<p><img src="/images/pontificia-universitas-gregoriana.jpg" alt="Pontifical Gregorian University" /></p>
<p>Next year, Curry On will be held in Barcelona, and I hope I’ll be able to attend
again.</p>
Wed, 20 Jul 2016 00:00:00 +0000https://mjj.io/2016/07/20/curry-on-a-retrospective/
https://mjj.io/2016/07/20/curry-on-a-retrospective/2015 in Review<p>This post is simply a reminder to myself of all the great things that have
happened during 2015.</p>
<h3 id="highlights">Highlights</h3>
<ul>
<li>In many ways, 2015 was a year of JavaScript to me. I visited my first
conference, <a href="https://www.react-europe.org">ReactEurope</a>, and went to a React
hackathon at the Mozilla office in Paris. In addition, I spent countless hours
learning <a href="http://flowtype.org/">Flow</a>, and implementing it into our existing
code base at work. I also familiarized myself with the wonders of
<a href="https://babeljs.io/">Babel</a>, and ECMAScript 2015, and I learned to apply, and
appreciate the ideas behind <a href="https://brandur.org/elegant-apis">JSON schemas</a>.</li>
<li>I submitted, and successfully defended my <a href="https://www.dropbox.com/s/g138w9klyneuajy/Mads-Mathias-Bachelor-Thesis.pdf?dl=0">bachelor’s
thesis</a>.
This is possibly the most challenging work I’ve accomplished so far. It is an
implementation of a lazy, functional programming language which can be
executed on two, separate stack machines that perform lazy evaluation using
two different strategies. It consists of a compiler written in OCaml, and the
aforementioned stack machines written in C. The source code is available on
<a href="https://github.com/majjoha/sloth">GitHub</a>, although sufficient documentation
is still severely lacking.</li>
</ul>
<h3 id="books-read">Books read</h3>
<p>Compared to 2014, I’ve read far fewer books in 2015 than I wanted to both in
terms of fiction, and technical books. In 2016, I hope to delve into Tropic of
Capricorn, Flaubert, and ideally also SICP which has been on my to-read list
since the beginning of 2014. These are the books I completed in 2015 in
chronological order:</p>
<ul>
<li><a href="https://www.goodreads.com/book/show/272642.Rip_it_Up_and_Start_Again">Simon Reynolds: Rip it Up and Start Again</a></li>
<li><a href="https://www.goodreads.com/book/show/38501.Ham_on_Rye">Charles Bukowski: Ham on Rye</a></li>
<li><a href="https://www.goodreads.com/book/show/3652866-the-implementation-of-functional-programming-languages">Simon L. Peyton Jones: The Implementation Of Functional Programming
Languages</a></li>
<li><a href="https://www.goodreads.com/book/show/486623.The_New_York_Trilogy">Paul Auster: The New York Trilogy</a></li>
<li><a href="https://www.goodreads.com/book/show/760.Memories_of_My_Melancholy_Whores">Gabriel García Márquez: Memories of My Melancholy Whores</a></li>
<li><a href="https://www.goodreads.com/book/show/4517763-junky">William S. Burroughs: Junky</a></li>
<li><a href="https://www.goodreads.com/book/show/9282423-learn-you-a-haskell-for-great-good">Miran Lipovača: Learn You a Haskell for Great Good!</a></li>
</ul>
<h3 id="favorite-records-of-the-year">Favorite records of the year</h3>
<p><a href="http://www.last.fm/user/bouleau/journal">I’ve listed</a> my favorite records of
the year for quite some years now. This year is no different.</p>
<ol>
<li><a href="http://www.allmusic.com/album/have-you-in-my-wilderness-mw0002860296">Julia Holter: Have You In My
Wilderness</a></li>
<li><a href="http://www.allmusic.com/album/ii-mw0002869328">Horsebeach: II</a></li>
<li><a href="http://www.allmusic.com/album/i-love-you-honeybear-mw0002777404">Father John Misty: I Love You, Honeybear</a></li>
<li><a href="http://www.allmusic.com/album/simple-songs-mw0002835921">Jim O’Rourke: Simple
Songs</a></li>
<li><a href="http://www.allmusic.com/album/beyond-belief-mw0002884480">Mark McGuire: Beyond Belief</a></li>
<li><a href="http://www.allmusic.com/album/depression-cherry-mw0002860587">Beach House: Depression Cherry</a></li>
<li><a href="http://www.allmusic.com/album/dream-all-over-mw0002877974">Gun Outfit: Dream All Over</a></li>
<li><a href="http://www.allmusic.com/album/summoning-suns-mw0002812289">James Blackshaw: Summoning Suns</a></li>
<li><a href="http://www.allmusic.com/album/blues-the-dark-paintings-of-mark-rothko-mw0002807887">Loren Connors: Blues: The Dark Paintings of “Mark Rothko”</a></li>
<li><a href="http://www.allmusic.com/album/vertigo-mw0002877481">The Necks: Vertigo</a></li>
</ol>
<p>I have a slight feeling that 2016 will include working with
<a href="https://facebook.github.io/relay/">Relay</a>, and
<a href="http://facebook.github.io/graphql/">GraphQL</a>. If time permits it, it would be
interesting to dabble in <a href="https://www.rust-lang.org/">Rust</a>,
<a href="http://www.idris-lang.org/">Idris</a>, <a href="http://racket-lang.org/">Racket</a>, and
<a href="https://elixir-lang.org">Elixir</a>, or even notably older programming languages
as <a href="https://en.wikipedia.org/wiki/Prolog">Prolog</a>, and
<a href="https://en.wikipedia.org/wiki/Forth_(programming_language)">Forth</a>.</p>
<p>Hopefully, I’ll have more time to practice music too, and perhaps travel a bit
as well. I’d also be fun to revisit my bachelor’s thesis, and refine the
language. I might give an update on the state of affairs next year. We’ll see.</p>
<p>Here’s to 2016!</p>
Fri, 01 Jan 2016 00:00:00 +0000https://mjj.io/2016/01/01/2015-in-review/
https://mjj.io/2016/01/01/2015-in-review/Ruby Extensions in C<p>Occasionally, we come across particular sections in our programs that need to be
exceptionally fast. Ruby allows us to write extensions in C, so that we can
delegate the heavy lifting. In this post, I’ll show you how easy it is to extend
Ruby with C by writing a trivial factorial function in C which we’ll be able to
call from Ruby.</p>
<p>We start out by creating the relevant directories and files.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>mkdir fact
<span class="nv">$ </span><span class="nb">cd </span>fact
<span class="nv">$ </span>mkdir ext
<span class="nv">$ </span>touch ext/extconf.rb ext/fact.c
</code></pre></div></div>
<p>In the <code class="highlighter-rouge">ext/extconf.rb</code> file, we require the
<a href="http://ruby-doc.org/stdlib-2.0.0/libdoc/mkmf/rdoc/MakeMakefile.html"><code class="highlighter-rouge">mkmf</code></a>
module which allows us to generate an applicable Makefile that compiles our C
code.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s2">"mkmf"</span>
<span class="n">create_makefile</span><span class="p">(</span><span class="s2">"fact"</span><span class="p">)</span>
</code></pre></div></div>
<p>Afterwards, we write the actual C program in the <code class="highlighter-rouge">ext/fact.c</code> file. It should
look like this:</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include "ruby.h"
</span>
<span class="kt">void</span> <span class="n">Init_fact</span><span class="p">();</span>
<span class="n">VALUE</span> <span class="n">fact</span><span class="p">(</span><span class="n">VALUE</span> <span class="n">self</span><span class="p">,</span> <span class="n">VALUE</span> <span class="n">n</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">Init_fact</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">VALUE</span> <span class="n">Fact</span> <span class="o">=</span> <span class="n">rb_define_module</span><span class="p">(</span><span class="s">"Fact"</span><span class="p">);</span>
<span class="n">rb_define_method</span><span class="p">(</span><span class="n">Fact</span><span class="p">,</span> <span class="s">"fact"</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">VALUE</span> <span class="nf">fact</span><span class="p">(</span><span class="n">VALUE</span> <span class="n">self</span><span class="p">,</span> <span class="n">VALUE</span> <span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">NUM2INT</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">factorial</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">x</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">factorial</span> <span class="o">*=</span> <span class="n">i</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">INT2NUM</span><span class="p">(</span><span class="n">factorial</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<p>First, we include the <code class="highlighter-rouge">ruby.h</code> header file, so that we can access the necessary
macros and functions. Ruby will by default execute our initializing function
<code class="highlighter-rouge">Init_fact</code>, so we define our <code class="highlighter-rouge">Fact</code> module in here with the function
<code class="highlighter-rouge">rb_define_module</code>. Additionally, we define a method <code class="highlighter-rouge">fact</code> in the <code class="highlighter-rouge">Fact</code>
module with the <code class="highlighter-rouge">rb_define_method</code> function. It takes a class, a method name, a
function and the number of arguments.</p>
<p>In the function <code class="highlighter-rouge">VALUE fact(VALUE self, VALUE n)</code>, we pass both <code class="highlighter-rouge">VALUE self</code> and
the number we want to take the factorial of. We convert the <code class="highlighter-rouge">VALUE n</code> to an
<code class="highlighter-rouge">int</code> with the <code class="highlighter-rouge">NUM2INT</code> macro, and convert the integer back to <code class="highlighter-rouge">Fixnum</code> when
returning the result.</p>
<p>We can then compile our program, and open <code class="highlighter-rouge">irb</code> to verify that it works as
expected:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>ruby ext/extconf.rb
creating Makefile
<span class="nv">$ </span>make
compiling ext/fact.c
linking shared-object fact.bundle
<span class="nv">$ </span>irb
irb<span class="o">(</span>main<span class="o">)</span>:001:0&gt; require_relative <span class="s2">"fact"</span>
<span class="o">=&gt;</span> <span class="nb">true
</span>irb<span class="o">(</span>main<span class="o">)</span>:002:0&gt; include Fact
<span class="o">=&gt;</span> Object
irb<span class="o">(</span>main<span class="o">)</span>:003:0&gt; fact<span class="o">(</span>5<span class="o">)</span>
<span class="o">=&gt;</span> 120
</code></pre></div></div>
<p>Utilizing C in our Ruby programs can be incredibly useful both in order to
achieve higher performance for specific parts of our application, but also if we
want interfacing with other C code. If you want to go further, I highly
recommend that you take a look at the
<a href="http://docs.ruby-lang.org/en/2.2.0/README_EXT.html"><code class="highlighter-rouge">README.EXT</code></a> documentation.</p>
Sat, 07 Feb 2015 00:00:00 +0000https://mjj.io/2015/02/07/ruby-extensions-in-c/
https://mjj.io/2015/02/07/ruby-extensions-in-c/Encrypting Files With GPG and Vim<p>I do most of my writing in Vim whether it is programming or editing ordinary
documents. The only two exceptions are journals and emails. For journals, I’ve
been using <a href="http://dayoneapp.com">Day One</a> for quite an extensive amount of time
now, but I’ve been considering to replace it with Vim. In order to replace Day
One, I need a way to effortlessly encrypt and decrypt text files. In this post,
I’ll show you how I’ve set up an environment that enables me to do so.</p>
<p>By default, Vim provides you with the ability to encrypt and decrypt files in a
quite simple manner. You open a file with <code class="highlighter-rouge">vim -x my-top-secret-document.md</code> or
the <code class="highlighter-rouge">:X</code> command, and then Vim prompts you for an encryption key. When you’ve
entered the encryption key twice, you are able to edit the document. If you try
to print the document with <code class="highlighter-rouge">cat</code> afterwards, you’ll see gibberish like
<code class="highlighter-rouge">VimCrypt~01!gd)�/�:�-(%)</code>, but if you open the file with Vim, the editor will
prompt you for the key phrase, and after entering the correct encryption key you
can read and edit the file. There is a caveat to this approach, however.
According to the <code class="highlighter-rouge">:X</code> help page, Vim has not been tested for robustness, and we
do not want swap files, the <code class="highlighter-rouge">viminfo</code> file or any other files for that matter to
expose our file contents, so instead we are going to rely on
<a href="https://www.gnupg.org/">GPG</a> and
<a href="http://learnvimscriptthehardway.stevelosh.com/chapters/12.html">autocommands</a>.</p>
<p>What we need to include in our <code class="highlighter-rouge">.vimrc</code> is the following autocommand group:</p>
<div class="language-viml highlighter-rouge"><div class="highlight"><pre class="highlight"><code>augroup encrypted
autocmd<span class="p">!</span>
autocmd <span class="nb">BufReadPre</span><span class="p">,</span><span class="nb">FileReadPre</span> *<span class="p">.</span>gpg <span class="k">set</span> viminfo<span class="p">=</span>
autocmd <span class="nb">BufReadPre</span><span class="p">,</span><span class="nb">FileReadPre</span> *<span class="p">.</span>gpg <span class="k">set</span> <span class="k">noswapfile</span> noundofile nobackup
autocmd <span class="nb">BufReadPost</span> *<span class="p">.</span>gpg <span class="p">:</span>%<span class="p">!</span>gpg <span class="p">--</span>decrypt <span class="m">2</span><span class="p">&gt;</span> <span class="sr">/dev/</span>null
autocmd <span class="nb">BufWritePre</span> *<span class="p">.</span>gpg <span class="p">:</span>%<span class="p">!</span>gpg <span class="p">-</span>ae <span class="p">--</span>default<span class="p">-</span>recipient<span class="p">-</span>self
autocmd <span class="nb">BufWritePost</span> *<span class="p">.</span>gpg <span class="k">u</span>
augroup END
</code></pre></div></div>
<p>Essentially, we disable auto-saving the <code class="highlighter-rouge">.viminfo</code> file, and then we disable
swap files, undo files and backup files. After the buffer is read, we decrypt
it with GPG, so that we are able to read the content in Vim. Before we
eventually save our file, we encrypt the entire file with the user ID of the
default key as the recipient of our message, and finally after writing the file
we undo the last action, so that the file is still readable to us.</p>
Tue, 27 Jan 2015 00:00:00 +0000https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/
https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/Curry Your Procs<p>Recently, I discovered that Ruby provides a rather esoteric <code class="highlighter-rouge">#curry</code> method for
<code class="highlighter-rouge">Proc</code>s that I’d like to examine in this post.</p>
<p>Currying basically means taking one function with multiple arguments and
converting it into a function that takes only one argument and returns another
function. The concept was originally coined by Moses Schönfinkel, and later
developed by Haskell Curry.</p>
<p>In Ruby, we might have a <code class="highlighter-rouge">Proc</code> taking multiple arguments:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">f</span> <span class="o">=</span> <span class="no">Proc</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="o">|</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">|</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span> <span class="p">}</span>
</code></pre></div></div>
<p>We can call <code class="highlighter-rouge">f</code> with all of its arguments by saying <code class="highlighter-rouge">f[1,2,3]</code> which would
evaluate to 6 in our case.</p>
<p>Currying <code class="highlighter-rouge">f</code> by hand would look like this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">curried_f</span> <span class="o">=</span> <span class="no">Proc</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">a</span><span class="o">|</span>
<span class="no">Proc</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">b</span><span class="o">|</span>
<span class="no">Proc</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span>
<span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>
<p>We can now evaluate our <code class="highlighter-rouge">Proc</code> by running <code class="highlighter-rouge">curried_f[1][2][3]</code> which would
evaluate to 6 exactly as in our previous example.</p>
<p>The ingenious reader have probably already guessed that <code class="highlighter-rouge">#curry</code> will take our
original <code class="highlighter-rouge">f</code> and turn it into <code class="highlighter-rouge">curried_f</code>. We can curry <code class="highlighter-rouge">f</code> in the following way
instead: <code class="highlighter-rouge">curried_f = f.curry</code>, and then finally call <code class="highlighter-rouge">curried_f[1][2][3]</code> which
will unsurprisingly return 6 as before.</p>
Tue, 04 Nov 2014 00:00:00 +0000https://mjj.io/2014/11/04/curry-your-procs/
https://mjj.io/2014/11/04/curry-your-procs/What We Talk About When We Talk About Rack<p>I was encouraged by <a href="https://twitter.com/jamiemhodge">Jamie Hodge</a> to give a
talk at this week’s <a href="http://www.copenhagenrb.dk/">Copenhagen Ruby Brigade</a>
meetup about Rack, and in this post, I’d like to give a recap on the subject.</p>
<p><a href="https://github.com/rack/rack">Rack</a> is essentially a minimal web server
interface that <a href="http://rubyonrails.org/">Rails</a>,
<a href="http://www.sinatrarb.com/">Sinatra</a>, <a href="http://lotusrb.org/">Lotus</a>,
<a href="http://cuba.is/">Cuba</a>, <a href="https://github.com/camping/camping">Camping</a> and
friends all heavily rely on. They do so in order not to interact directly with
the lower levels of the socket communication, and instead they distribute this
particular work for Rack, so they can focus on other parts of the architecture.
The main benefit of Rack is that you can write your applications once, and run
them everywhere. Almost all Ruby servers support Rack, so you can easily power
up your application without having to tailor it to a specific platform.</p>
<p>A Rack application is basically an object that responds to <code class="highlighter-rouge">#call</code>, accepts
<code class="highlighter-rouge">env</code> as its only argument and returns an array containing the HTTP status code,
the headers and an object that responds to <code class="highlighter-rouge">#each</code>. Using a stabby lambda, a
simple Rack application printing “Hello, world” to its users could look like
this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'rack'</span>
<span class="n">app</span> <span class="o">=</span> <span class="o">-&gt;</span><span class="p">(</span><span class="n">env</span><span class="p">)</span> <span class="k">do</span>
<span class="p">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{</span><span class="s2">"Content-type"</span> <span class="o">=&gt;</span> <span class="s2">"text/html"</span><span class="p">},</span> <span class="p">[</span><span class="s2">"&lt;h1&gt;Hello, world!&lt;/h1&gt;"</span><span class="p">]]</span>
<span class="k">end</span>
<span class="no">Rack</span><span class="o">::</span><span class="no">Server</span><span class="p">.</span><span class="nf">start</span><span class="p">(</span><span class="ss">app: </span><span class="n">app</span><span class="p">)</span>
</code></pre></div></div>
<p>At some point, we probably want to extract our logic into its own class. This is
quite manageable to do, since we simply need an object that responds to <code class="highlighter-rouge">#call</code>
and takes <code class="highlighter-rouge">env</code> as its only argument. We could for instance end up with the
following:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'rack'</span>
<span class="k">class</span> <span class="nc">SuperAdvancedWebApp</span>
<span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="p">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{</span><span class="s2">"Content-type"</span> <span class="o">=&gt;</span> <span class="s2">"text/html"</span><span class="p">},</span> <span class="p">[</span><span class="n">env</span><span class="p">]]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="no">Rack</span><span class="o">::</span><span class="no">Server</span><span class="p">.</span><span class="nf">start</span><span class="p">(</span><span class="ss">app: </span><span class="no">SuperAdvancedWebApp</span><span class="p">.</span><span class="nf">new</span><span class="p">)</span>
</code></pre></div></div>
<p>Except from being extracted into a class, the aforementioned application is
slightly different in that it actually returns the result of the <code class="highlighter-rouge">env</code> to its
visitors, so that we can investigate the contents of the current environment
from the browser.</p>
<p>For learning purposes, I’ve built a small web framework utilizing Rack that
discovers how we can effortlessly build a framework similar to Rails, Sinatra
and so forth which I’ve named Dolphy, and you can study the source code on
<a href="https://github.com/majjoha/dolphy">GitHub</a>.</p>
<p>It is possible to avoid the dependency of Rack by using TCP sockets as I
presented in my previous post, “<a href="/2014/09/17/investigating-sockets/">Investigating
sockets</a>”. The incredibly small framework
<a href="https://github.com/pachacamac/busker">busker</a> is an attempt at building a web
framework without the dependency of Rack. I keep a branch called
<a href="https://github.com/majjoha/dolphy/tree/majjoha/rackless">majjoha/rackless</a> in
the Dolphy repository where I try to remove Rack from the dependencies of the
project in a similar manner.</p>
<p>Furthermore, Rack itself is also in an exciting state at this moment, as <a href="https://twitter.com/tenderlove">Aaron
Patterson</a> who is also a Rails core contributor
recently took over the development of the project. This is what he said about
the <code class="highlighter-rouge">env</code> hash back in August:</p>
<blockquote class="twitter-tweet" data-conversation="none" lang="en"><p><a href="https://twitter.com/jcoglan">@jcoglan</a> not just that, the mutable env hash passed everywhere is the bane of my existence.</p>&mdash; Aaron Patterson (@tenderlove) <a href="https://twitter.com/tenderlove/status/502479098975764480">August 21, 2014</a></blockquote>
<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>I am thrilled to see that we’ll finally get rid of the <code class="highlighter-rouge">env</code> hash from our Rack
applications. In the repository,
<a href="https://github.com/tenderlove/the_metal">the_metal</a>, he keeps a spike for
thoughts about Rack 2.0, and according to the examples in the project, it’ll
slightly change how we use Rack to build our web applications:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Application</span>
<span class="k">def</span> <span class="nf">call</span> <span class="n">req</span><span class="p">,</span> <span class="n">res</span>
<span class="n">res</span><span class="p">.</span><span class="nf">write_head</span> <span class="mi">200</span><span class="p">,</span> <span class="s1">'Content-Type'</span> <span class="o">=&gt;</span> <span class="s1">'text/plain'</span>
<span class="n">res</span><span class="p">.</span><span class="nf">write</span> <span class="s2">"Hello World</span><span class="se">\n</span><span class="s2">"</span>
<span class="n">res</span><span class="p">.</span><span class="nf">finish</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="nb">require</span> <span class="s1">'the_metal/puma'</span>
<span class="n">server</span> <span class="o">=</span> <span class="no">TheMetal</span><span class="p">.</span><span class="nf">create_server</span> <span class="no">Application</span><span class="p">.</span><span class="nf">new</span>
<span class="n">server</span><span class="p">.</span><span class="nf">listen</span> <span class="mi">9292</span><span class="p">,</span> <span class="s1">'0.0.0.0'</span>
</code></pre></div></div>
<p>I highly welcome the change, and I find this new way of interacting with the
request and response directly much more elegant than fiddling with the <code class="highlighter-rouge">env</code>
hash as we’re used to. It is going to be interesting to see how these changes
will affect all the existing frameworks that we use today.</p>
Sat, 27 Sep 2014 00:00:00 +0000https://mjj.io/2014/09/27/what-we-talk-about-when-we-talk-about-rack/
https://mjj.io/2014/09/27/what-we-talk-about-when-we-talk-about-rack/Investigating Sockets<p>During this summer, I’ve revisited an old side project of mine which is a
Ruby-based micro framework for web development. Like most web frameworks in
Ruby, this heavily depends on Rack which can be extremely helpful in a lot
of ways, but you do not learn all the lower levels of how the server handles
requests from clients and so forth that I find somewhat intriguing. This
ultimately led me to investigate the different sockets that the Ruby standard
library provides and in general improve my understanding on sockets. This post
covers some of my findings from the process.</p>
<p>Sockets are the endpoints of bidirectional communication channels. The Ruby
standard library has six different socket classes: <code class="highlighter-rouge">UNIXSocket</code>, <code class="highlighter-rouge">UDPSocket</code>,
<code class="highlighter-rouge">TCPSocket</code>, <code class="highlighter-rouge">Socket</code>, <code class="highlighter-rouge">IPSocket</code>, and they all inherit from the sixth socket
class <code class="highlighter-rouge">BasicSocket</code>. The class hierarchy for the sockets in Ruby looks like
this:</p>
<p><img src="/images/sockets-class-hierarchy.png" alt="Class hierarchy" /></p>
<p>Starting from the bottom, TCP is the most commonly used protocol on the Internet
as it offers error correction, and like UDP it belongs to the transport layer.
Furthermore, TCP sockets guarantee delivery which means that it will resend
packets if needed and stop the data flow until packets are successfully
transferred, so TCP sockets are considered very reliable.</p>
<p>Implementing a small single-threaded web server that prints the current time to
the user could be done this way utilizing the <code class="highlighter-rouge">TCPServer</code>.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'socket'</span>
<span class="n">server</span> <span class="o">=</span> <span class="no">TCPServer</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="mi">2345</span><span class="p">)</span>
<span class="kp">loop</span> <span class="k">do</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="nf">accept</span>
<span class="n">client</span><span class="p">.</span><span class="nf">gets</span>
<span class="n">client</span><span class="p">.</span><span class="nf">puts</span> <span class="s2">"Time is </span><span class="si">#{</span><span class="no">Time</span><span class="p">.</span><span class="nf">now</span><span class="si">}</span><span class="s2">"</span>
<span class="n">client</span><span class="p">.</span><span class="nf">close</span>
<span class="k">end</span>
</code></pre></div></div>
<p>Accessing <a href="http://localhost:2345">http://localhost:2345</a> in the browser would
then return the current time to the user. This is actually what I started out
with when I was replacing Rack with sockets, and from here you need to figure
out how to match URLs from the user, handle multiple users, send/receive POST
and GET parameters, send the necessary headers to the browser and so on. This
process reveals how much work Rack actually does for you, but I enjoy seeing how
much I can do without it as well.</p>
<p>Moving on to UDP, this protocol, on the other hand, is considered unreliable,
but it offers speed why it is often used for streaming purposes. Also, UDP
packets are remarkably smaller than TCP packets. Unlike TCP, UDP does not
provide any error correction or flow control, so errors will be present.</p>
<p>Since UDP does not do retransmissions, we might also lose content, or content
might be ordered the wrong way as UDP does not guarantee in-order delivery.
Thus, TCP is quite obviously the most appropriate protocol of the two to rely on
when implementing a web framework without Rack.</p>
<p>Implementing a simple chat server and client using <code class="highlighter-rouge">UDPSocket</code> could be done the
following way:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># udp-server.rb</span>
<span class="nb">require</span> <span class="s1">'socket'</span>
<span class="n">socket</span> <span class="o">=</span> <span class="no">UDPSocket</span><span class="p">.</span><span class="nf">new</span>
<span class="n">socket</span><span class="p">.</span><span class="nf">bind</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="mi">33333</span><span class="p">)</span>
<span class="kp">loop</span> <span class="k">do</span>
<span class="n">data</span><span class="p">,</span> <span class="n">address</span> <span class="o">=</span> <span class="n">socket</span><span class="p">.</span><span class="nf">recvfrom</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"From address: '</span><span class="si">#{</span><span class="n">address</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="s1">','</span><span class="p">)</span><span class="si">}</span><span class="s2">', message: </span><span class="si">#{</span><span class="n">data</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">socket</span><span class="p">.</span><span class="nf">close</span>
</code></pre></div></div>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># udp-client.rb</span>
<span class="nb">require</span> <span class="s1">'socket'</span>
<span class="n">socket</span> <span class="o">=</span> <span class="no">UDPSocket</span><span class="p">.</span><span class="nf">new</span>
<span class="k">while</span> <span class="n">message</span> <span class="o">=</span> <span class="nb">gets</span><span class="p">.</span><span class="nf">chomp</span>
<span class="n">socket</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">33333</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">socket</span><span class="p">.</span><span class="nf">close</span>
</code></pre></div></div>
<p>The server creates a new <code class="highlighter-rouge">UDPSocket</code> and binds it to localhost on port 3333.
From here, it will simply run a loop listening for messages and print them. The
client works in a similar fashion. It creates a new <code class="highlighter-rouge">UDPSocket</code>, runs a loop
that receives messages from the standard input which it then sends to our
server.</p>
<p>Moving up the class hierarchy, we have <code class="highlighter-rouge">UNIXSocket</code>, <code class="highlighter-rouge">IPSocket</code> and <code class="highlighter-rouge">Socket</code>
where the latter will not be elaborated. A <code class="highlighter-rouge">UNIXSocket</code> basically represents a
UNIX domain stream client, and what characterizes a UNIX domain stream is that
it does not use any underlying network protocol for communication, so it has
less overhead. It solely exists inside a single computer, and processes
communicating with a UNIX domain stream needs to be on the same computer too.
For these reasons, the <code class="highlighter-rouge">UNIXSocket</code> is obviously not suited for a web framework,
but the <a href="https://gist.github.com/ryanlecompte/1619490"><code class="highlighter-rouge">UnixSocketForker</code></a> by
Ryan LeCompte is an interesting usage of the <code class="highlighter-rouge">UNIXSocket</code> in a Ruby context.
Finally, the <code class="highlighter-rouge">IPSocket</code> which is also the superclass of <code class="highlighter-rouge">TCPSocket</code> and
<code class="highlighter-rouge">UDPSocket</code> is not so exciting on its own and therefore a little difficult to
leverage, but one common usage for the <code class="highlighter-rouge">IPSocket</code> is looking up the IP address
of the host which we could do in the following way:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'socket'</span>
<span class="n">ip</span> <span class="o">=</span> <span class="no">IPSocket</span><span class="p">.</span><span class="nf">getaddress</span><span class="p">(</span><span class="no">Socket</span><span class="p">.</span><span class="nf">gethostname</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">ip</span> <span class="c1">#=&gt; "192.168.0.100"</span>
</code></pre></div></div>
<p>Investigating sockets have been an interesting journey so far, and I’ll
certainly need to investigate them even more for this project in particular. I
hope you’ve found this whirlwind tour of the Ruby sockets useful, and feel free
to ping me if you’ve any questions.</p>
Wed, 17 Sep 2014 00:00:00 +0000https://mjj.io/2014/09/17/investigating-sockets/
https://mjj.io/2014/09/17/investigating-sockets/Kinesis Advantage Macros<p>While I was visiting the US in July, I bought a Kinesis Advantage keyboard, and
it has been one of the better investments I’ve done lately. Not only is it
extremely comfortable to use, but it is also easily configurable through its
remapping and macro system. Some keys, however, do not behave the way I would
expect them to by default on OS X. One of these keys is the <kbd>PrintScr</kbd>
key, but you can solve it almost effortlessly via a macro. You need to do the
following:</p>
<ul>
<li>Press and hold <kbd>Program</kbd> and <kbd>F11/Macro</kbd> (lights on keyboard
will flash repeatedly).</li>
<li>Press and release the macro trigger, in this case, <kbd>PrintScr</kbd>
(flashing lights should slow down).</li>
<li>Type your macro. Press and hold <kbd>Cmd</kbd>, then <kbd>Shift</kbd>, and as
you are holding both down, tap the <kbd>3</kbd> key and release.</li>
<li>Exit by repeating step 1 (lights should stop flashing).</li>
</ul>
<p>If you prefer to take screenshots of specific areas of the screen, you might
want to map this to <kbd>Cmd</kbd>, <kbd>Shift</kbd> and <kbd>4</kbd> instead.</p>
Fri, 12 Sep 2014 00:00:00 +0000https://mjj.io/2014/09/12/kinesis-advantage-macros/
https://mjj.io/2014/09/12/kinesis-advantage-macros/The Joy of .agignore<p>For searching files, I normally use <a href="https://github.com/ggreer/the_silver_searcher/">The Silver
Searcher</a> (or Ag as it is
commonly shortened), and I’ve set up <a href="https://github.com/kien/ctrlp.vim">CtrlP</a>
in Vim to use Ag as well for fuzzy file search. CtrlP is, however, terribly slow
when working with larger projects if you do not scope your search properly. For
quite some time, I’ve been trying to solve this issue, but I recently stumbled
upon the solution to my agonizing problem.</p>
<p>First of all, since CtrlP uses Ag, it uses the <code class="highlighter-rouge">.agignore</code> file for ignoring
specific folders, file types, etc. I needed to exclude especially the
<code class="highlighter-rouge">vendor/bundle</code> folder from the results as it currently contains over 150.000
files that I am not interested in at all. Despite the fact that I configured
<code class="highlighter-rouge">.agignore</code> accordingly, CtrlP still did not scope my search as expected.</p>
<p>After traversing several pages of search results on Google, I finally found the
solution mentioned in <a href="https://github.com/ggreer/the_silver_searcher/issues/367">an issue on
GitHub</a>. Apparently, I
had to update Ag to version 0.22 as earlier versions of Ag does not respect the
<code class="highlighter-rouge">.agignore</code> file, and it now works like a charm.</p>
Tue, 05 Aug 2014 00:00:00 +0000https://mjj.io/2014/08/05/the-joy-of-agignore/
https://mjj.io/2014/08/05/the-joy-of-agignore/