Jekyll2019-02-22T04:42:25+00:00https://nithinbekal.com/feed.xmlNithin BekalNithin Bekal's blog about programming - Ruby, Rails, Vim, Elixir.Make Delegated Methods Private in Rails2019-02-07T18:00:00+00:002019-02-07T18:00:00+00:00https://nithinbekal.com/posts/rails-delegate<p>Recently,
I came across some of my old code
that uses Rails’
<a href="https://api.rubyonrails.org/classes/Module.html#method-i-delegate"><code class="highlighter-rouge">delegate</code> method</a>.
Take a look at the following example,
where I’m delegating a couple of methods
to an instance variable,
but want to make them private.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">UserDecorator</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
<span class="vi">@user</span> <span class="o">=</span> <span class="n">user</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">full_name</span>
<span class="s2">"</span><span class="si">#{</span><span class="n">first_name</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">last_name</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="n">delegate</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span><span class="p">,</span> <span class="ss">to: :@user</span>
<span class="k">end</span>
</code></pre></div></div>
<p>My intention was to make the <code class="highlighter-rouge">first_name</code> and <code class="highlighter-rouge">last_name</code> methods private.
Only <code class="highlighter-rouge">full_name</code> was supposed to be public here.
Let’s see if this works as intended.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">first_name: </span><span class="s1">'John'</span><span class="p">,</span> <span class="ss">last_name: </span><span class="s1">'Doe'</span><span class="p">)</span>
<span class="n">decorated_user</span> <span class="o">=</span> <span class="no">UserDecorator</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
<span class="n">decorated_user</span><span class="p">.</span><span class="nf">full_name</span> <span class="c1">#=&gt; John Doe</span>
<span class="n">decorated_user</span><span class="p">.</span><span class="nf">first_name</span> <span class="c1">#=&gt; John</span>
<span class="n">decorated_user</span><span class="p">.</span><span class="nf">last_name</span> <span class="c1">#=&gt; Doe</span>
<span class="n">decorated_user</span><span class="p">.</span><span class="nf">methods</span> <span class="o">-</span> <span class="no">Object</span><span class="p">.</span><span class="nf">instance_methods</span>
<span class="c1">#=&gt; [:full_name, :last_name, :first_name]</span>
</code></pre></div></div>
<p>As you can see,
we can call both the delegated methods.
They were supposed to be private!
What happened here?</p>
<p>The problem is that
putting <code class="highlighter-rouge">delegate</code> under the <code class="highlighter-rouge">private</code> scope
has no effect.
<strong>Methods in Ruby
have no way of knowing
that the <code class="highlighter-rouge">private</code> visibility scope is set
when they are called from within a class.</strong>
<code class="highlighter-rouge">delegate</code> uses <code class="highlighter-rouge">module_eval</code> to define
new methods with these names,
but it doesn’t know
that it must make these methods private.</p>
<p>So what do we do
if we wish to delegate a method
without making it a part of the class’ public interface?
Let’s dig into how <code class="highlighter-rouge">delegate</code> works.
In cases like this,
I like to use the
<a href="https://github.com/pry/pry">pry console</a>
to interactively inspect the behavior of the class.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pry</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="no">UserDecorator</span>
<span class="c1"># This pry command puts us within the scope of the UserDecorator class.</span>
<span class="n">pry</span><span class="o">&gt;</span> <span class="n">delegate</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span><span class="p">,</span> <span class="ss">to: :@user</span>
<span class="c1">#=&gt; [:first_name, :last_name]</span>
<span class="n">pry</span><span class="o">&gt;</span> <span class="nb">instance_methods</span>
<span class="c1">#=&gt; [:full_name, :last_name, :first_name]</span>
<span class="n">pry</span><span class="o">&gt;</span> <span class="kp">private</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span>
<span class="n">pry</span><span class="o">&gt;</span> <span class="nb">instance_methods</span>
<span class="c1">#=&gt; [:full_name]</span>
</code></pre></div></div>
<p><code class="highlighter-rouge">delegate</code> returns the list of the delegated method names.
We can call <code class="highlighter-rouge">private</code> with the method names
to make them private.
This means we can explicitly make the methods private like this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">UserDecorator</span>
<span class="c1"># def initialize...</span>
<span class="n">delegate</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span><span class="p">,</span> <span class="ss">to: :@user</span>
<span class="kp">private</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span>
<span class="k">end</span>
</code></pre></div></div>
<p>Now, how can we avoid duplicating the list of method names?
Since <code class="highlighter-rouge">delegate</code> returns the list of method names,
we can pass the list to <code class="highlighter-rouge">private</code>
using the splat operator.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">UserDecorator</span>
<span class="c1"># def initialize...</span>
<span class="kp">private</span> <span class="o">*</span><span class="n">delegate</span><span class="p">(</span><span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span><span class="p">,</span> <span class="ss">to: :@user</span><span class="p">)</span>
<span class="k">end</span>
</code></pre></div></div>
<p>Although this prevents duplication,
I don’t find this syntax particularly intuitive.
Luckily,
Rails 6 is introducing
a new <code class="highlighter-rouge">private: true</code> keyword argument
to give us a cleaner syntax.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Rails 6+</span>
<span class="n">delegate</span> <span class="ss">:first_name</span><span class="p">,</span> <span class="ss">:last_name</span><span class="p">,</span> <span class="ss">to: :@user</span><span class="p">,</span> <span class="ss">private: </span><span class="kp">true</span>
</code></pre></div></div>
<p>However,
as I write this post,
Rails 6 isn’t out yet.
If you’re on Rails 5.2 or below,
you’ll have to use
the <code class="highlighter-rouge">private *delegate(...)</code> syntax for now.</p>
<h3 id="links">Links</h3>
<ul>
<li>Rails PR:
<a href="https://github.com/rails/rails/pull/31944">delegate to, with <code class="highlighter-rouge">private: true</code> option</a></li>
</ul>Recently, I came across some of my old code that uses Rails’ delegate method. Take a look at the following example, where I’m delegating a couple of methods to an instance variable, but want to make them private.Favorite Books of 20182019-01-24T19:25:18+00:002019-01-24T19:25:18+00:00https://nithinbekal.com/posts/books-2018<p>29 books and about 10,000 pages.
Once again
I fell short of my target
of reading a book every week,
but it’s almost identical to
last year’s reading stats.
Here’s some of my favorite books of the year.</p>
<h3 id="non-fiction">Non-fiction</h3>
<p><strong><a href="https://amzn.to/2WbaZJg">Mindset</a> (Carol S Dweck)</strong></p>
<p>Anyone who thinks,
“I don’t have the talent to learn…”
ought to read this book
and understand the concept of the growth mindset.
It shows the impact
of treating your skills
as something you can develop
rather than something you’re born with.</p>
<p><strong><a href="https://amzn.to/2Wg4IMf">HBR Guide to Getting the Mentoring You Need</a></strong></p>
<p>This was an important read for me this year.
It’s a great, short read on mentoring,
consisting of essays
from Harvard Business Review.
It highlighted things I could do
to get more out of mentors,
and
what I’ve been doing wrong
when mentoring others.</p>
<p><strong><a href="https://www.sandimetz.com/99bottles/">99 Bottles of OOP</a>
(Sandi Metz, Katrina Owen)</strong></p>
<p>A few months ago,
I attended
Sandi Metz’s fantastic Object Oriented Design course,
which took us through
the topics covered in this book.
On the surface,
the book is about writing a program
to print a silly song.
But the amount of complexity
that it unearths in such a simple problem
is astounding.
It takes you through a refactoring journey,
explaining every step along the way,
almost as if you’re pairing with the authors.</p>
<p>It’s great for every experience level.
For beginners,
it will teach you about refactoring,
testing
and writing great code.
And for more experienced folks,
it will expose the bad habits
you’ve picked up over the years.</p>
<h3 id="fiction">Fiction</h3>
<p><strong><a href="https://amzn.to/2DvfI0Z">The Way of Kings</a>
(Brandon Sanderson)</strong></p>
<p>This was an incredible book.
I was looking for a good epic fantasy series
after finishing
<a href="https://amzn.to/2S3xEYO">The Wheel of Time series</a>.
I raced through the 1200+ pages,
finishing the final 400 pages
in a single sitting.
I’m constantly amazed by
the many different worlds and magic systems
that Sanderson has created.
Highly recommended for fans of epic fantasy,
especially Wheel of Time fans.</p>
<p><strong><a href="https://amzn.to/2MqHEWT">The Devotion of Suspect X</a>
(Keigo Higashino)</strong></p>
<p>It’s an unconventional detective story,
because it starts by telling us
who committed the crime.
The rest of the book is about
the suspect covering up their tracks.
Yet it manages to lull you
into thinking where the story is going
before throwing up unexpected plot twists.
The characters were one-dimensional,
but it makes up for it
with a gripping storyline,
and almost makes you root for the suspect.</p>
<p><strong><a href="https://amzn.to/2WeeGhn">Artemis</a>
(Andy Weir)</strong></p>
<p><a href="https://amzn.to/2FUPWVL">The Martian</a>
is one of my favorite sci-fi novels,
so I’ve been excited about this second book by Andy Weir.
It’s not at the same level as The Martian,
but has elements I liked in that one:
the science it touches upon is very plausible,
and the plot maintains a frenetic page.
The setting has moved from Mars
to a human colony on the Moon,
and the protagonist is basically
a female version of the one from The Martian -
a resourceful, wisecracking character
in trouble far away from Earth.</p>
<p><strong><a href="https://amzn.to/2UcMOYW">The Emperor’s Soul</a>
(Brandon Sanderson)</strong></p>
<p>This is a fantastic novella.
Sanderson manages to
build an entire new world and magic system
in just 175 pages.
Highly recommended for Sanderson fans.</p>
<p><strong><a href="https://amzn.to/2Tb4Omz">Mistborn: The Alloy Era series</a>
(Brandon Sanderson)</strong></p>
<p>This is made up of
the 4th to 6th books
in the Mistborn series -
<a href="https://amzn.to/2Tb4Omz">The Alloy of Law</a>,
<a href="https://amzn.to/2MuLTAs">Shadows of Self</a>,
and
<a href="https://amzn.to/2RIv0Z8">The Bands of Mourning</a>.
The first
<a href="https://amzn.to/2S1zqd3">Mistborn trilogy</a>
was set in a medieval world,
while this one is set 300 years later.
Here the same world has evolved
to the Industrial Age,
with railroads, electricity and guns.</p>
<p>This felt a bit different
from the original trilogy -
parts of it feel like a suspense novel
as much as a fantasy one.
It also revealed a lot more
about the Mistborn world’s connection
to the rest of the Cosmere Universe -
the shared universe
in which all of Sanderson’s worlds exist.</p>
<h3 id="2019">2019</h3>
<p>Almost every year,
I give myself a target
to finish 52 books,
ie. a book every week.
For 2019,
I’m trying a less ambitious
target of 30.
I think
this will also be the year
I switch to reading ebooks
more that printed books.
I finally got myself
a <a href="https://amzn.to/2TfSjX7">Kindle Oasis</a>.
That should make it easier
to read while traveling.</p>
<h3 id="links">Links</h3>
<ul>
<li>Previous lists:
<a href="/posts/favorite-books-2017/">2017</a>,
<a href="/posts/favorite-books-2016/">2016</a>,
<a href="/posts/favorite-books-2015/">2015</a>,
<a href="/posts/favorite-books-2014/">2014</a>,
<a href="/posts/favorite-books-2013/">2013</a></li>
</ul>29 books and about 10,000 pages. Once again I fell short of my target of reading a book every week, but it’s almost identical to last year’s reading stats. Here’s some of my favorite books of the year.What’s new in Ruby 2.62019-01-19T17:00:00+00:002019-01-19T17:00:00+00:00https://nithinbekal.com/posts/ruby-2-6<p>Ruby 2.6 was released on Christmas day,
and brought a few interesting new features.
Here’s a quick summary of what’s changed.
As with my summaries of previous versions
(<a href="/posts/ruby-2-5-features/">2.5</a>,
<a href="/posts/ruby-2-4-features/">2.4</a>,
<a href="/posts/ruby-2-3-features/">2.3</a>)
I’m only summarizing
features that I find most interesting.
For a complete list of changes,
take a look at the
<a href="https://www.ruby-lang.org/en/news/2018/12/25/ruby-2-6-0-released/">changelog</a>.</p>
<h3 id="endless-ranges">Endless ranges</h3>
<p>A new syntax was introduced to represent an endless range.
This will be useful when matching ranges in case statements.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">case</span> <span class="n">n</span>
<span class="k">when</span> <span class="mi">1</span><span class="o">..</span><span class="mi">9</span> <span class="k">then</span> <span class="s1">'Single digit'</span>
<span class="k">when</span> <span class="mi">10</span><span class="o">..</span><span class="mi">99</span> <span class="k">then</span> <span class="s1">'Two digit'</span>
<span class="k">when</span> <span class="mi">100</span><span class="o">..</span> <span class="k">then</span> <span class="s1">'Three or more'</span>
<span class="k">end</span>
</code></pre></div></div>
<p>Previously, <code class="highlighter-rouge">(1..)</code> would be represented as <code class="highlighter-rouge">(1..Float::INFINITY)</code>.
Not many people are familiar that you can
<a href="/posts/ruby-infinity/">represent infinity like this</a>,
so this syntax is a definite improvement.</p>
<h3 id="composing-procs-lambdas-and-methods">Composing procs, lambdas and methods</h3>
<p><code class="highlighter-rouge">#&gt;&gt;</code> and <code class="highlighter-rouge">#&lt;&lt;</code> methods were added to <code class="highlighter-rouge">Proc</code> and <code class="highlighter-rouge">Method</code> objects
to allow composing them into a new method.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">square</span> <span class="o">=</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span> <span class="p">}</span>
<span class="n">add_2</span> <span class="o">=</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">2</span> <span class="p">}</span>
<span class="p">(</span><span class="n">square</span> <span class="o">&gt;&gt;</span> <span class="n">add_2</span><span class="p">).</span><span class="nf">call</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1">#=&gt; 27</span>
<span class="p">(</span><span class="n">square</span> <span class="o">&lt;&lt;</span> <span class="n">add_2</span><span class="p">).</span><span class="nf">call</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1">#=&gt; 49</span>
</code></pre></div></div>
<p>With the <code class="highlighter-rouge">#&gt;&gt;</code> operation,
the number gets <code class="highlighter-rouge">square</code>d first and then <code class="highlighter-rouge">add_2</code> is performed
on the result of the first proc call.
In case of <code class="highlighter-rouge">#&lt;&lt;</code>,
the order in which the procs are called is reversed.</p>
<p>I wish we didn’t have
two different ways of composing procs,
and only had <code class="highlighter-rouge">#&gt;&gt;</code>.
But composition is an important addition to Ruby
that will allow us to write in a more functional style.</p>
<h3 id="kernelthen"><code class="highlighter-rouge">Kernel#then</code></h3>
<p>This isn’t a new feature,
but an alias for <code class="highlighter-rouge">Kernel#yield_self</code>
which was introduced in Ruby 2.5,
and allows us to chain operations
into pipelines like this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">isbn</span> <span class="o">=</span> <span class="s1">'978-1-93778-549-9'</span>
<span class="n">isbn</span><span class="p">.</span><span class="nf">gsub</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
<span class="p">.</span><span class="nf">then</span> <span class="p">{</span> <span class="o">|</span><span class="n">isbn</span><span class="o">|</span> <span class="no">URI</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="no">API_URL</span><span class="si">}</span><span class="s2">?q=isbn:</span><span class="si">#{</span><span class="n">isbn</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">then</span> <span class="p">{</span> <span class="o">|</span><span class="n">uri</span><span class="o">|</span> <span class="no">Net</span><span class="ss">:HTTP</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">then</span> <span class="p">{</span> <span class="o">|</span><span class="n">json_response</span><span class="o">|</span> <span class="no">JSON</span><span class="p">.</span><span class="nf">parse</span><span class="p">(</span><span class="n">json_response</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">then</span> <span class="p">{</span> <span class="o">|</span><span class="n">response</span><span class="o">|</span> <span class="n">response</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="s1">'items'</span><span class="p">,</span> <span class="s1">'volumeInfo'</span><span class="p">)</span> <span class="p">}</span>
</code></pre></div></div>
<p><em>(Shameless plug:
I recorded a guest episode on RubyTapas
showing a series of refactors leading to the code above -
<a href="https://www.rubytapas.com/2019/01/08/yield_self/">watch it here</a>.)</em></p>
<h3 id="enumerablechain-and-enumerator"><code class="highlighter-rouge">Enumerable#chain</code> and <code class="highlighter-rouge">Enumerator#+</code></h3>
<p>This returns an <code class="highlighter-rouge">Enumerator::Chain</code> object,
which works as a single enumerator.</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">3</span><span class="p">).</span><span class="nf">chain</span><span class="p">((</span><span class="mi">5</span><span class="o">..</span><span class="mi">7</span><span class="p">),</span> <span class="p">[</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">]).</span><span class="nf">to_a</span>
<span class="c1">#=&gt; [1, 2, 3, 5, 6, 7, 9, 10]</span>
<span class="n">list</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">3</span><span class="p">).</span><span class="nf">each</span> <span class="o">+</span> <span class="p">(</span><span class="mi">5</span><span class="o">..</span><span class="mi">7</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="mi">9</span><span class="o">..</span><span class="mi">10</span><span class="p">)</span>
<span class="n">list</span><span class="p">.</span><span class="nf">to_a</span>
<span class="c1">#=&gt; [1, 2, 3, 5, 6, 7, 9, 10]</span>
</code></pre></div></div>
<h3 id="merge-multiple-hashes">Merge multiple hashes</h3>
<p><code class="highlighter-rouge">Hash#merge</code> only took one argument till 2.5.
The only way to merge more than two hashes
was to chain calls to <code class="highlighter-rouge">#merge</code>.
You can now pass multiple hashes to the method.</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="p">{</span> <span class="ss">a: </span><span class="mi">1</span> <span class="p">}</span>
<span class="n">bar</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">b: </span><span class="mi">2</span> <span class="p">}</span>
<span class="n">baz</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">c: </span><span class="mi">3</span> <span class="p">}</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">merge</span><span class="p">(</span><span class="n">bar</span><span class="p">,</span> <span class="n">baz</span><span class="p">)</span> <span class="c1">#=&gt; { a: 1, b: 2, c: 3 }</span>
<span class="c1"># ruby 2.5</span>
<span class="c1"># foo.merge(bar).merge(baz)</span>
</code></pre></div></div>
<h3 id="arrayunion-and-arraydifference"><code class="highlighter-rouge">Array#union</code> and <code class="highlighter-rouge">Array#difference</code></h3>
<p>These methods work just like
the <code class="highlighter-rouge">|</code> and <code class="highlighter-rouge">&amp;</code> operators.
The difference is that they can accept multiple arguments
and are easier to chain.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</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="n">b</span> <span class="o">=</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="n">a</span><span class="p">.</span><span class="nf">union</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1">#=&gt; [1, 2, 3, 4]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">difference</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1">#=&gt; [1, 1, 2]</span>
</code></pre></div></div>
<h3 id="enumerableto_h-with-block"><code class="highlighter-rouge">Enumerable#to_h</code> with block</h3>
<p><code class="highlighter-rouge">Enumerable#to_h</code> now accepts a block that maps keys to values.
This allows us to transform hashes
without creating an intermediate array using <code class="highlighter-rouge">map</code>,
or using the harder-to-read <code class="highlighter-rouge">reduce</code> syntax.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">hash</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">foo: </span><span class="mi">2</span><span class="p">,</span> <span class="ss">bar: </span><span class="mi">3</span> <span class="p">}</span>
<span class="nb">hash</span><span class="p">.</span><span class="nf">to_h</span> <span class="p">{</span> <span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="p">[</span><span class="n">k</span><span class="p">.</span><span class="nf">upcase</span><span class="p">,</span> <span class="n">v</span><span class="o">*</span><span class="n">v</span><span class="p">]</span> <span class="p">}</span> <span class="c1">#=&gt; { FOO: 4, BAR: 9 }</span>
<span class="c1"># ruby 2.5:</span>
<span class="c1"># hash.map { |k, v| [k.upcase, v*v] }.to_h</span>
<span class="c1"># hash.reduce({}) { |result, (k, v)| result.merge(k.upcase =&gt; v) }</span>
</code></pre></div></div>
<h3 id="enumeratorarithmeticsequence"><code class="highlighter-rouge">Enumerator::ArithmeticSequence</code></h3>
<p>This subclass of <code class="highlighter-rouge">Enumerator</code>
was introduced to represent
objects created by calling <code class="highlighter-rouge">step</code>
on <code class="highlighter-rouge">Range</code> and <code class="highlighter-rouge">Numeric</code> objects.
This makes it possible to do things like
check equality of sequences.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">r1</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">step</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">r2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">step</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">r1</span> <span class="o">==</span> <span class="n">r2</span> <span class="c1">#=&gt; true if 2.6, false in 2.5</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">step</span> <span class="o">==</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="p">).</span><span class="nf">step</span> <span class="c1">#=&gt; true</span>
</code></pre></div></div>
<h3 id="changes-in-range-behavior">Changes in <code class="highlighter-rouge">Range</code> behavior</h3>
<p><strong>The <code class="highlighter-rouge">%</code> operator</strong>
has been added as an alias to <code class="highlighter-rouge">step</code>.
So <code class="highlighter-rouge">(1..10) % 2</code> is equivalent to <code class="highlighter-rouge">(1..10).step(2)</code>.</p>
<p><strong><code class="highlighter-rouge">Range#===</code> now uses uses <code class="highlighter-rouge">#cover?</code> instead of <code class="highlighter-rouge">#include?</code>.</strong>
<code class="highlighter-rouge">case</code> statements internally use <code class="highlighter-rouge">===</code>,
so this could lead to subtly different behavior
in case statements.</p>
<p><code class="highlighter-rouge">(a..b).cover?(x)</code> checks if <code class="highlighter-rouge">a &lt;= x &lt; b</code>,
whereas <code class="highlighter-rouge">(a..b).include?(x)</code>
iterates through the range
and checks if any element equals <code class="highlighter-rouge">x</code>.
This allows us to do things like:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">case</span> <span class="no">DateTime</span><span class="p">.</span><span class="nf">now</span>
<span class="k">when</span> <span class="no">Date</span><span class="p">.</span><span class="nf">today</span><span class="o">..</span><span class="no">Date</span><span class="p">.</span><span class="nf">today</span><span class="o">+</span><span class="mi">1</span>
<span class="nb">puts</span> <span class="s1">'matched'</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s1">'not matched'</span>
<span class="k">end</span>
</code></pre></div></div>
<p><strong>Note</strong>:
In a previous version of this article,
I used a string range in the example.
For backwards compatibility,
the behavior of string ranges hasn’t changed.</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">'c'</span><span class="p">).</span><span class="nf">cover?</span><span class="p">(</span><span class="s1">'bb'</span><span class="p">)</span> <span class="c1">#=&gt; true</span>
<span class="p">(</span><span class="s1">'a'</span><span class="o">..</span><span class="s1">'c'</span><span class="p">).</span><span class="nf">include?</span><span class="p">(</span><span class="s1">'bb'</span><span class="p">)</span> <span class="c1">#=&gt; false</span>
<span class="k">case</span> <span class="s1">'bb'</span>
<span class="k">when</span> <span class="s1">'a'</span><span class="o">..</span><span class="s1">'c'</span> <span class="k">then</span> <span class="s1">'matched'</span>
<span class="k">else</span> <span class="s1">'not matched'</span>
<span class="k">end</span>
<span class="c1">#=&gt; 'not matched'</span>
</code></pre></div></div>
<h3 id="exception-keyword-argument-for-kernel-methods"><code class="highlighter-rouge">exception</code> keyword argument for <code class="highlighter-rouge">Kernel</code> methods</h3>
<p>An <code class="highlighter-rouge">exception</code> keyword argument
was added for some <code class="highlighter-rouge">Kernel</code> methods
like <code class="highlighter-rouge">Integer</code>, <code class="highlighter-rouge">Float</code> and <code class="highlighter-rouge">system</code>.
For the <code class="highlighter-rouge">Numeric</code> methods,
we can now pass <code class="highlighter-rouge">exception: false</code>
to avoid raising when parsing an invalid value,
and to instead return <code class="highlighter-rouge">nil</code>.</p>
<p><code class="highlighter-rouge">Kernel#system</code>, on the other hand,
accepts <code class="highlighter-rouge">exception: true</code>
to raise
if the command exits with non-zero exit status
(instead of returning <code class="highlighter-rouge">false</code>)
or if command execution fails
(instead of returning <code class="highlighter-rouge">nil</code>).</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">Integer</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span> <span class="c1">#=&gt; nil</span>
<span class="no">Float</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span> <span class="c1">#=&gt; nil</span>
<span class="nb">system</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">true</span><span class="p">)</span> <span class="c1">#=&gt; exception</span>
</code></pre></div></div>
<h3 id="performance-improvements">Performance improvements</h3>
<p>The biggest news on the performance front
is that a JIT implementation,
called
<a href="https://medium.com/@k0kubun/the-method-jit-compiler-for-ruby-2-6-388ee0989c13">MJIT</a>,
was merged into Ruby.
This can be enabled
using the <code class="highlighter-rouge">--jit</code> flag.</p>
<p>MJIT has led to speedups in micro-benchmarks,
but isn’t mature enough yet
to work for larger codebases like Rails apps.
It is even slower for Rails than the non-JIT version.</p>
<p>However, this is an important step
towards the Ruby 3x3 goal,
which is to make Ruby 3
at least 3x faster that Ruby 2.0.
Currently Ruby 2.6 with JIT is
<a href="https://medium.com/@k0kubun/ruby-2-6-jit-progress-and-future-84e0a830ecbf">nearly 2.5x faster than 2.0</a>
on the Optcarrot benchmark,
which is used
to compare the performance
of Ruby releases
for the 3x3 goal.</p>
<p>Aside from this,
a second garbage collection heap,
called <a href="https://bugs.ruby-lang.org/issues/14858">Transient Heap</a>,
was introduced,
which reduces memory usage
and improves GC speed
of short lived objects.
<code class="highlighter-rouge">Proc.call</code> and <code class="highlighter-rouge">block.call</code></p>
<h3 id="better-introspection">Better introspection</h3>
<p>Ruby’s introspection abilities have been improved,
which the introduction
of the <code class="highlighter-rouge">RubyVM::AbstractSyntaxTree</code> class,
which lets us parse Ruby code into AST.
<code class="highlighter-rouge">Binding.source_location</code> was also added,
which returns an array containing
the file name and the line number
where it was called.</p>
<h3 id="other-changes">Other changes</h3>
<ul>
<li><a href="/posts/ruby-flip-flop/">The flip flop operator</a>
is finally deprecated,
and will probably be removed in Ruby 3.</li>
<li>Bundler is now a default gem,
so you no longer have to do <code class="highlighter-rouge">gem install bundler</code>
after installing Ruby.
Rubygems 3.0.1 was also merged into Ruby.</li>
<li><code class="highlighter-rouge">Array#filter</code> was added
as an alias for <code class="highlighter-rouge">#select</code>.</li>
<li>Previous versions of ruby warned
if you had an <code class="highlighter-rouge">else</code> clause in a <code class="highlighter-rouge">begin</code> block
without a <code class="highlighter-rouge">rescue</code>.
This will cause a syntax error in 2.6.</li>
</ul>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">begin</span>
<span class="nb">puts</span> <span class="s1">'begin'</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s1">'else'</span>
<span class="k">end</span>
<span class="c1"># Ruby 2.6</span>
<span class="c1">#=&gt; foo.rb:5: else without rescue is useless</span>
<span class="c1"># Ruby 2.5</span>
<span class="c1">#=&gt; foo.rb:7: warning: else without rescue is useless</span>
<span class="c1">#=&gt; begin</span>
<span class="c1">#=&gt; else</span>
</code></pre></div></div>
<h3 id="links">Links</h3>
<ul>
<li><a href="https://github.com/ruby/ruby/blob/trunk/doc/NEWS-2.6.0">NEWS for 2.6.0</a></li>
<li><a href="https://rubyreferences.github.io/rubychanges/2.6.html">Ruby 2.6 changes list</a>
by Victor Shepelev,
has a comprehensive list of changes,
along with links
to the relevant discussions
on the Ruby issue tracker.</li>
</ul>Ruby 2.6 was released on Christmas day, and brought a few interesting new features. Here’s a quick summary of what’s changed. As with my summaries of previous versions (2.5, 2.4, 2.3) I’m only summarizing features that I find most interesting. For a complete list of changes, take a look at the changelog.Favorite Books of 20172018-02-12T17:27:33+00:002018-02-12T17:27:33+00:00https://nithinbekal.com/posts/favorite-books-2017<p>I read 28 books, and over 10,000 pages in 2017
– a long way behind my target of 52 books.
The move to Ottawa, and the settling in, got in the way,
but now that the winter is here,
I should be able to get back
to that long to-read list.</p>
<h2 id="non-fiction">Non fiction</h2>
<p><strong><a href="https://amzn.to/2Wgr0Of">The Ascent of Man</a>
(Jacob Bronowski)</strong></p>
<p>This is a remarkable book
that uses the evolution of science
to trace human history.
Starting with the use of stone as weapons by homo erectus,
all the way to quantum physics,
this is a fascinating picture of the rise of civilization.
Quite a bit like what Carl Sagan’s Cosmos is to the history of the Universe.</p>
<p><strong><a href="https://amzn.to/2Mu67dO">The Selfish Gene</a>
(Richard Dawkins)</strong></p>
<p>An absorbing read on evolutionary biology
from the perspective of individual genes.
Especially loved the game theory based explanations
that explain natural selection.
One of the most engaging science books I’ve read.</p>
<p><strong><a href="https://amzn.to/2Td8cgS">Astrophysics for People in a Hurry</a>
(Neil deGrasse Tyson)</strong></p>
<p>As a long time listener of Tyson’s Startalk podcast,
I’ve heard a lot of this stuff covered on the show.
Tyson has an amazing ability to convey the vastness of the Universe
in a way that could get anyone interested in the topic.
My only complaint is the use of imperial units over metric,
making it confusing for us non-Americans.</p>
<p><strong><a href="https://amzn.to/2Ta7jWc">Made in Japan</a>
(Akio Morita)</strong></p>
<p>The story of the founding and growth of Sony.
The book traces Morita’s early life during WW2,
the early days of Sony and its emergence as a cutting edge technology company,
and about Japanese business and management styles.
Morita writes well, and effortlessly switches between these topics,
so it feels less like memoirs,
and more like a book about business.
Excellent read if you want to read about
how Japanese management styles compare to those of other countries.</p>
<h2 id="fiction">Fiction</h2>
<p><strong><a href="https://amzn.to/2MwLJbO">Animal Farm</a>
(George Orwell)</strong></p>
<p>A book with talking animals may not sound serious,
but it’s a very accurate representation of the political class.
Although based on the Russian revolution and Stalin,
it captures the shenanigans of today’s politicians,
even the supposedly democratic ones.
I can think of one or two world leaders
whose brazen distortion of reality
isn’t too far off the behavior of the pig leaders on Animal Farm.</p>
<p><strong><a href="https://amzn.to/2Mu6lla">Mistborn: The Final Empire</a>
(Brandon Sanderson)</strong></p>
<p>I’ve wanted to read Sanderson’s books
ever since I read his fantastic conclusion to the Wheel of Time series,
and this book didn’t disappoint.
It’s fast paced,
but doesn’t sacrifice world building and character development for it.
The magical system is well developed,
and allows you to form your own theories
on where the story is going.
I’m usually bored by long descriptions of fights,
but Sanderson is especially skilled
at keeping you hanging on to every word.</p>
<p><strong><a href="https://amzn.to/2MwMsd2">Fuzzy Nation</a>
(John Scalzi)</strong></p>
<p>My favorite scifi novel of the year.
The characters are complex, funny and memorable
– even when they are creatures that don’t speak,
like the protagonist’s pet dog.
Towards the end it turns into something of a legal thriller
that debates what it means to be sentient.</p>
<p><strong><a href="https://amzn.to/2Ud3AqZ">Seveneves</a>
(Neal Stephenson)</strong></p>
<p>Intriguing premise –
the moon mysteriously explodes,
and the Earth has two years before the debris kills all of humanity.
Explores how existing space technology can be jury-rigged
to move as many people to space as possible.</p>
<p>Stephenson’s books can be slow and meandering,
but this one was interesting from page 1.
Has a lot of details about the workings of the International Space Station.
Going from long explanations on orbital mechanics on one page
to the machinations of power hungry politicians on the next,
it was one hell of a ride.
I’ve been looking for good hard-sci-fi novels since reading The Martian,
and this one is definitely worth a read for fans of that book</p>
<p>The final third is speculative fiction about set 5000 years in the future,
that is effectively a sequel to the first 2/3rds.
I didn’t enjoy this as much as the story set in near future,
which has a lot of details about space technology.
Despite the disappointing final third,
I think it was a terrific sci fi novel overall.</p>
<p><strong><a href="https://amzn.to/2MsxfK6">The Name of the Wind</a>
(Patrick Rothfuss)</strong></p>
<p>First person narrative in an epic fantasy novel is unusual,
but here it works.
Has some great world building,
although doesn’t depart from the typical Tolkienian fantasy style much.
Sadly, there are few supporting characters that are well fleshed out.
There are also some chapters
that would be better off in a bad romance novel.
Not as good as I was expecting based on the reviews,
but a decent fantasy novel all the same.</p>
<p><strong><a href="https://amzn.to/2TbXqav">Inkheart</a>
(Cornelia Funke)</strong></p>
<p>What if someone had the power
to literally bring characters to life by reading a book?
Bibliophiles will definitely enjoy this one.
This is technically a children’s book,
but like most good children’s novels,
it can be enjoyed by adults as well.</p>
<p><strong><a href="https://amzn.to/2Mw0gVf">All You Need is Kill</a>
(Hiroshi Sakurazaka)</strong></p>
<p>Nice, fast paced scifi thriller with a great premise -
a soldier who is fighting alien invaders
accidentally resets time every time he dies,
and wakes up the previous day.
I’m glad that the movie adaptation (Edge of Tomorrow)
changed the plot enough that it didn’t spoil this book too much.</p>
<p><strong><a href="https://amzn.to/2Wj9Uzk">Ready Player One</a>
(Ernest Cline)</strong></p>
<p>Set in 2044, people are using a massive virtual reality universe
to escape the bleakness of the real world.
The VR’s creator leaves all his wealth to whoever can find the easter egg
he left in the simulation.
The writing style and the dialogue
keeps reminding me that it’s a YA novel, but a fun read so far.
The people in the story, unfortunately,
feel more like video game characters than the real world people
that are playing the games.
Someone who grew up around the 80s geek culture might appreciate this more,
and that probably explains the massive fan following.
Entertaining read, nonetheless.</p>
<p><strong><a href="https://amzn.to/2MtNdDP">Dune</a>
(Frank Herbert)</strong></p>
<p>It’s great in so many levels –
you can read it for the fast paced epic adventure,
the scifi novel,
the messiah story reminiscent of many fantasy series,
or even Herbert’s philosophical ruminations.
Although it’s a scifi novel,
it has a lot in common with fantasy -
some parts of it reminded me of the Wheel of Time.
Took me a few chapters to really get into it,
but in the end I couldn’t put it down.</p>
<h3 id="2018">2018</h3>
<p>In 2018,
I’m hoping to read 30 pages every day.
That’s easier said than done, though –
I’ve missed a few days already.
I didn’t read a lot of programming books in 2017,
so I’m hoping to pick up more of those this year.</p>
<h3 id="links">Links</h3>
<ul>
<li>Previous lists:
<a href="/posts/favorite-books-2016/">2016</a>,
<a href="/posts/favorite-books-2015/">2015</a>,
<a href="/posts/favorite-books-2014/">2014</a>,
<a href="/posts/favorite-books-2013/">2013</a></li>
<li><a href="/notes/books/">Other books lists</a></li>
</ul>I read 28 books, and over 10,000 pages in 2017 – a long way behind my target of 52 books. The move to Ottawa, and the settling in, got in the way, but now that the winter is here, I should be able to get back to that long to-read list.What’s New in Ruby 2.5?2017-12-27T10:12:42+00:002017-12-27T10:12:42+00:00https://nithinbekal.com/posts/ruby-2-5-features<p>The Ruby core team has traditionally released a new version of Ruby
on Christmas day,
and this Christmas,
we got Ruby 2.5.
A couple of years ago,
I started posting a summary of Ruby changes
(<a href="/posts/ruby-2-4-features/">2.4</a>,
<a href="/posts/ruby-2-3-features/">2.3</a>),
and this year’s version is here.</p>
<p><em>(This is only a list of things
I found most interesting about this release,
so if you want a complete list,
<a href="https://github.com/ruby/ruby/blob/trunk/doc/NEWS-2.5.0">take a look at the changelog</a>.)</em></p>
<h2 id="rescue-in-doend-blocks">Rescue in do/end blocks</h2>
<p>If you needed to rescue an exception in a block,
the only way to do that was to put it in a begin/end block.
Now, you can rescue exceptions inside all do/end blocks.</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">5</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
<span class="n">do_something</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">rescue</span> <span class="no">SomeError</span> <span class="o">=&gt;</span> <span class="n">e</span>
<span class="nb">puts</span> <span class="n">e</span>
<span class="k">next</span>
<span class="k">end</span>
</code></pre></div></div>
<h2 id="top-level-constant-lookup-is-removed">Top level constant lookup is removed</h2>
<p>In previous versions,
if Ruby couldn’t find a constant in the current scope,
it would fallback to the top level constant,
and emit a warning.
This has changed in 2.5,
and this will now cause an exception.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span> <span class="k">end</span>
<span class="k">class</span> <span class="nc">Bar</span><span class="p">;</span> <span class="k">end</span>
<span class="c1"># Ruby 2.4</span>
<span class="no">Foo</span><span class="o">::</span><span class="no">Bar</span>
<span class="c1"># warning: toplevel constant Bar referenced by Foo::Bar</span>
<span class="c1">#=&gt; Bar</span>
<span class="c1"># Ruby 2.5</span>
<span class="no">Foo</span><span class="o">::</span><span class="no">Bar</span>
<span class="c1"># NameError</span></code></pre></figure>
<h2 id="backtrace-in-reverse-order">Backtrace in reverse order</h2>
<p>When printing to console,
Ruby will display the backtrace in reverse order.
You will also see the error message in bold and underlined.
This is an experimental feature,
and the order won’t be changed when printing to logs.</p>
<p>When working on Rails projects,
it is common to have to scroll a long way back
to find the line in the backtrace
that shows the line where the exception happened.
By reversing the backtrace,
you get the context on the line causing the exception
without having to scroll back.</p>
<h2 id="kernelyield_self"><code class="highlighter-rouge">Kernel#yield_self</code></h2>
<p>This method passes an object to a block
and returns the value returned by the block.
The use of pipelines is common in Elixir,
so I’ll translate some
<a href="https://github.com/nithinbekal/google_books.ex/blob/1fe98c7d2bbc2f84b1dedcdc7b248a3b85c3030d/lib/google_books.ex#L19">code</a>
from one of my Elixir packages to Ruby
to demonstrate this feature.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">isbn</span> <span class="o">=</span> <span class="s2">"0-306-40615-2"</span>
<span class="n">isbn</span><span class="p">.</span><span class="nf">gsub</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
<span class="p">.</span><span class="nf">yield_self</span> <span class="p">{</span> <span class="o">|</span><span class="n">isbn</span><span class="o">|</span> <span class="no">URI</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="no">API_URL</span><span class="si">}</span><span class="s2">?q=isbn:</span><span class="si">#{</span><span class="n">isbn</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">yield_self</span> <span class="p">{</span> <span class="o">|</span><span class="n">uri</span><span class="o">|</span> <span class="no">Net</span><span class="ss">:HTTP</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">yield_self</span> <span class="p">{</span> <span class="o">|</span><span class="n">json_response</span><span class="o">|</span> <span class="no">JSON</span><span class="p">.</span><span class="nf">parse</span><span class="p">(</span><span class="n">json_response</span><span class="p">)</span> <span class="p">}</span>
<span class="p">.</span><span class="nf">yield_self</span> <span class="p">{</span> <span class="o">|</span><span class="n">response</span><span class="o">|</span> <span class="n">response</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="s1">'items'</span><span class="p">,</span> <span class="s1">'volumeInfo'</span><span class="p">)</span> <span class="p">}</span></code></pre></figure>
<p>The Ruby community has many people
who are interested in Elixir,
so this could end up being a popular feature.
I wonder if this could have been an operator,
but it’s still an interesting addition to Ruby.</p>
<h2 id="standard-gems">Standard Gems</h2>
<p>Ruby comes with a lot of things bundled into the standard library,
and this makes it hard to push bug fixes after a Ruby version is released.
To solve this, many libraries are being gemified,
and released independently of Ruby.
Libraries like cmath and webrick have been extracted this way.</p>
<p>This doesn’t mean you can’t use Webrick the next time you install Ruby, though.
The gems being extracted will be shipped with Ruby as default gems,
but you also have the option of installing the latest version through Rubygems.</p>
<ul>
<li><a href="https://stdgems.org/2.5.0/">List of standard gems</a></li>
<li><a href="https://bugs.ruby-lang.org/projects/ruby/wiki/StdlibGem">Gemifying the standard library</a></li>
<li><a href="https://bugs.ruby-lang.org/issues/5481">Discussion on Ruby bug tracker</a></li>
</ul>
<h2 id="string-methods">String methods</h2>
<p><strong><code class="highlighter-rouge">String#-@</code></strong>: deduplicates unfrozen strings.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">s</span> <span class="o">=</span> <span class="o">-</span> <span class="s1">'foo'</span> <span class="c1">#=&gt; 'foo'</span>
<span class="n">s</span><span class="p">.</span><span class="nf">frozen?</span> <span class="c1">#=&gt; true</span></code></pre></figure>
<p><strong><code class="highlighter-rouge">delete_prefix</code></strong> and <strong><code class="highlighter-rouge">delete_suffix</code></strong>
(along with the bang <code class="highlighter-rouge">!</code> versions)</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="s1">'Mr. Smith'</span><span class="p">.</span><span class="nf">delete_prefix</span><span class="p">(</span><span class="s1">'Mr. '</span><span class="p">)</span> <span class="c1">#=&gt; 'Smith'</span>
<span class="s1">'Wellington St.'</span><span class="p">.</span><span class="nf">delete_suffix</span><span class="p">(</span><span class="s1">' St.'</span><span class="p">)</span> <span class="c1">#=&gt; 'Wellington'</span></code></pre></figure>
<p><strong><code class="highlighter-rouge">grapheme_clusters</code></strong></p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">s</span> <span class="o">=</span> <span class="s2">"a</span><span class="se">\u</span><span class="s2">0300"</span> <span class="c1">#=&gt; "à"</span>
<span class="n">s</span><span class="p">.</span><span class="nf">grapheme_clusters</span> <span class="c1">#=&gt; ["à"]</span>
<span class="n">s</span><span class="p">.</span><span class="nf">codepoints</span> <span class="c1">#=&gt; [97, 768]</span></code></pre></figure>
<h2 id="array-methods">Array methods</h2>
<p><code class="highlighter-rouge">#append</code> and <code class="highlighter-rouge">#prepend</code> methods have been added,
which are aliases for <code class="highlighter-rouge">#push</code> and <code class="highlighter-rouge">#unshift</code> respectively.
These aliases were already introduced in Rails via ActiveSupport,
but it’s nice to see them introduced into Ruby.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">list</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">]</span> <span class="c1">#=&gt; ['b', 'c']</span>
<span class="n">list</span><span class="p">.</span><span class="nf">prepend</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="c1">#=&gt; ['a', 'b', 'c']</span>
<span class="n">list</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="s1">'d'</span><span class="p">)</span> <span class="c1">#=&gt; ['a', 'b', 'c', 'd']</span>
<span class="n">list</span> <span class="c1">#=&gt; ['a', 'b', 'c', 'd']</span></code></pre></figure>
<h2 id="hash-methods">Hash methods</h2>
<p><code class="highlighter-rouge">#slice</code> and <code class="highlighter-rouge">#transform_keys</code> have been added,
which are also originally from ActiveSupport.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{</span> <span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">,</span> <span class="ss">c: </span><span class="mi">3</span><span class="p">,</span> <span class="ss">d: </span><span class="mi">4</span> <span class="p">}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="ss">:a</span><span class="p">,</span> <span class="ss">:b</span><span class="p">)</span>
<span class="c1">#=&gt; { a: 1, b: 2 }</span>
<span class="n">h</span><span class="p">.</span><span class="nf">transform_keys</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:to_s</span><span class="p">)</span>
<span class="c1">#=&gt; {"a"=&gt;1, "b"=&gt;2, "c"=&gt;3, "d"=&gt;4}</span></code></pre></figure>
<h2 id="goodbye-ubygemsrb">Goodbye, <code class="highlighter-rouge">ubygems.rb</code>!</h2>
<p>This change will have zero impact on how you use Ruby,
but I was curious why we had an <code class="highlighter-rouge">ubygems.rb</code> file in stdlib,
so I wanted to mention it now that it’s been removed.</p>
<p>Ruby has a <code class="highlighter-rouge">-r</code> flag that lets you require a library,
e.g. <code class="highlighter-rouge">ruby -rmath</code> to <code class="highlighter-rouge">require 'math'</code>.
The rubygems file was named <code class="highlighter-rouge">ubygems.rb</code>
so that the flag could be used as
<code class="highlighter-rouge">-rubygems</code> instead of <code class="highlighter-rouge">-rrubygems</code>.
This file hasn’t been needed since Ruby 1.9,
and has now been removed from stdlib.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://github.com/ruby/ruby/blob/trunk/doc/NEWS-2.5.0">NEWS for Ruby 2.5.0</a>.</li>
<li><a href="https://docs.ruby-lang.org/en/trunk/index.html">Ruby trunk docs</a></li>
</ul>The Ruby core team has traditionally released a new version of Ruby on Christmas day, and this Christmas, we got Ruby 2.5. A couple of years ago, I started posting a summary of Ruby changes (2.4, 2.3), and this year’s version is here.What’s new in Rails 5.1?2017-03-04T09:09:03+00:002017-03-04T09:09:03+00:00https://nithinbekal.com/posts/rails-5.1-features<p>It’s time for another version of Rails.
The first beta of <a href="http://weblog.rubyonrails.org/2017/2/23/Rails-5-1-beta1/">Rails 5.1 was released</a> a few days ago,
and it introduces some goodies that will make things a lot easier
for people who need to work with JS in their Rails apps.
Here’s a quick tour of the new features:</p>
<h2 id="yarn-is-part-of-the-default-stack">Yarn is part of the default stack</h2>
<p>In my previous post, I wrote about
<a href="/posts/yarn-rails/">using yarn with Rails</a>.
With Rails 5.1, you no longer have to do anything -
it will be supported out of the box.
Yarn is the equivalent of Bundler for JS libraries,
and this will make it a lot easier to manage those dependencies.</p>
<h2 id="webpack-option">Webpack option</h2>
<p>Rails’ official <a href="https://github.com/rails/webpacker">webpacker</a> gem
now makes it easy to integrate webpack with Rails apps.
Rails has a <code class="highlighter-rouge">--webpack</code> option to add this gem to Gemfile,
and set up the project to work with Webpack.</p>
<h2 id="easily-add-react-angular-or-vuejs">Easily add React, Angular or Vue.js</h2>
<p>With the introduction of webpacker,
you also have to option of easily setting up
React, Angular or Vue.js as part of our Rails project.
Webpacker takes care installing the dependencies,
and creating the configuration files.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c"># For new projects</span>
rails new foobar <span class="nt">--webpack</span><span class="o">=</span>react
<span class="c"># in existing projects</span>
rails webpacker:install:react</code></pre></figure>
<h2 id="system-tests-with-capybara">System tests with Capybara</h2>
<p>Rails now has <code class="highlighter-rouge">ActionDispatch::IntegrationTest</code> that lets you
use Capybara for integration tests, without needing any extra configuration.
By default, it uses Chrome browser to run the tests,
but can be configured to use Poltergeist, Selenium or Firefox.
Here’s an example taken from <a href="http://edgeapi.rubyonrails.org/classes/ActionDispatch/SystemTestCase.html">Rails docs</a>:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="nb">require</span> <span class="s1">'application_system_test_case'</span>
<span class="k">class</span> <span class="nc">Users</span><span class="o">::</span><span class="no">CreateTest</span> <span class="o">&lt;</span> <span class="no">ApplicationSystemTestCase</span>
<span class="nb">test</span> <span class="s2">"adding a new user"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="n">users_path</span>
<span class="n">click_on</span> <span class="s1">'New User'</span>
<span class="n">fill_in</span> <span class="s1">'Name'</span><span class="p">,</span> <span class="ss">with: </span><span class="s1">'Arya'</span>
<span class="n">click_on</span> <span class="s1">'Create User'</span>
<span class="n">assert_text</span> <span class="s1">'Arya'</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<h2 id="encrypted-secrets">Encrypted secrets</h2>
<p>The recommended way to store secrets, such as passwords and secret tokens,
was to put them in environment variables.
Now there is an option to set up an encrypted secrets file
which can be checked into source control.
You will have a master key, stored outside the repo,
which can be used to decrypt the keys.</p>
<h2 id="declarative-exception-handling-in-activejob">Declarative exception handling in ActiveJob</h2>
<p>ActiveJob now has a cleaner API for handling retries.
Here are some of the
<a href="http://edgeapi.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html">examples for <code class="highlighter-rouge">retry_on</code> from the docs</a>:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">RemoteServiceJob</span> <span class="o">&lt;</span> <span class="no">ActiveJob</span><span class="o">::</span><span class="no">Base</span>
<span class="n">retry_on</span> <span class="no">CustomAppException</span> <span class="c1"># defaults to 3s wait, 5 attempts</span>
<span class="n">retry_on</span> <span class="no">AnotherCustomAppException</span><span class="p">,</span> <span class="ss">wait: </span><span class="o">-&gt;</span><span class="p">(</span><span class="n">executions</span><span class="p">)</span> <span class="p">{</span> <span class="n">executions</span> <span class="o">*</span> <span class="mi">2</span> <span class="p">}</span>
<span class="n">retry_on</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Deadlocked</span><span class="p">,</span> <span class="ss">wait: </span><span class="mi">5</span><span class="p">.</span><span class="nf">seconds</span><span class="p">,</span> <span class="ss">attempts: </span><span class="mi">3</span>
<span class="n">retry_on</span> <span class="no">Net</span><span class="o">::</span><span class="no">OpenTimeout</span><span class="p">,</span> <span class="ss">wait: :exponentially_longer</span><span class="p">,</span> <span class="ss">attempts: </span><span class="mi">10</span>
<span class="k">def</span> <span class="nf">perform</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="c1"># ...</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<h2 id="bigint-primary-keys">BIGINT primary keys</h2>
<p>If you’re using Postgres or MySQL, primary keys are BIGINT by default.
This is not something you need to worry about unless you have tables
that are likely to have more that <code class="highlighter-rouge">2**31 - 1</code> rows in a table,
but making this the default will make this one less thing to worry about.
Jon McCartie, who added this feature,
<a href="http://www.mccartie.com/2016/12/05/rails-5.1.html">has written in detail about this</a>.</p>
<h2 id="new-form_with-helper">New <code class="highlighter-rouge">form_with</code> helper</h2>
<p>A new <a href="http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with"><code class="highlighter-rouge">form_with</code></a>
helper has been added which unifies the <code class="highlighter-rouge">form_for</code> and <code class="highlighter-rouge">form_tag</code> methods.
Examples:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># Instead of form_for</span>
<span class="o">&lt;</span><span class="sx">%= form_with model: @post do |form| %&gt;
&lt;%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="o">%&gt;</span>
<span class="o">&lt;</span><span class="sx">% end </span><span class="o">%&gt;</span>
<span class="c1"># Instead of form_tag</span>
<span class="o">&lt;</span><span class="sx">%= form_with scope: :post, url: posts_path do |form| %&gt;
&lt;%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="o">%&gt;</span>
<span class="o">&lt;</span><span class="sx">% end </span><span class="o">%&gt;</span></code></pre></figure>
<h2 id="parameterized-mailers">Parameterized mailers</h2>
<p><code class="highlighter-rouge">ActionMailer::Parameterized</code> provides a way to perform setup with <code class="highlighter-rouge">before_action</code>,
by passing in parameters to the method.</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="no">InvitationMailer</span><span class="p">.</span><span class="nf">with</span><span class="p">(</span><span class="ss">invitee: </span><span class="n">foo</span><span class="p">).</span><span class="nf">account_invitation</span><span class="p">.</span><span class="nf">deliver_later</span>
<span class="k">class</span> <span class="nc">InvitationMailer</span> <span class="o">&lt;</span> <span class="no">ApplicationMailer</span>
<span class="n">before_action</span> <span class="p">{</span> <span class="vi">@invitee</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:invitee</span><span class="p">]</span> <span class="p">}</span>
<span class="k">def</span> <span class="nf">account_invitation</span>
<span class="n">mail</span> <span class="ss">to: </span><span class="vi">@invitee</span><span class="p">.</span><span class="nf">email</span><span class="p">,</span> <span class="c1"># ...</span>
<span class="k">end</span>
<span class="k">end</span></code></pre></figure>
<ul>
<li><a href="http://edgeapi.rubyonrails.org/classes/ActionMailer/Parameterized.html">ActionMailer::Parameterized docs</a></li>
</ul>
<h2 id="new-tag-helper">New tag helper</h2>
<p>The ActionView <code class="highlighter-rouge">tag</code> helper has been changed to provide a cleaner syntax
(<a href="http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag">see docs</a>).</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># Old</span>
<span class="n">tag</span><span class="p">(</span><span class="s1">'div'</span><span class="p">,</span> <span class="ss">class: </span><span class="s1">'foo'</span><span class="p">)</span>
<span class="c1"># New</span>
<span class="n">tag</span><span class="p">.</span><span class="nf">div</span><span class="p">(</span><span class="s1">'Hello, world!'</span><span class="p">,</span> <span class="ss">class: </span><span class="s1">'foo'</span><span class="p">)</span>
<span class="n">tag</span><span class="p">.</span><span class="nf">div</span> <span class="n">tag</span><span class="p">.</span><span class="nf">p</span><span class="p">(</span><span class="s1">'Hello world!'</span><span class="p">)</span></code></pre></figure>
<h2 id="goodbye-jquery">Goodbye, jquery</h2>
<p>Rails UJS no longer depends on jquery,
so jquery-rails will no longer be included by default.
You might still need jquery in your app, but now you can add it via Yarn:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">bin/yarn add jquery</code></pre></figure>
<h2 id="upgrading">Upgrading</h2>
<p>For a minor release version, Rails 5.1 has quite a few interesting changes,
but despite that upgrading from Rails 5.0 shouldn’t be too difficult.
I tried upgrading a small project and was up and running in 10 minutes or so.</p>
<p>Integrating front end libs has always been a pain in Rails,
since there was not standard way to do it.
So making Yarn and Webpack first class citizens
of the Rails stack in awesome news.
<a href="/posts/capybara-minitest-rails/">Capybara too is something I found
myself configuring all the time in Rails projects</a>,
and not having to do that is another win.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="http://weblog.rubyonrails.org/2017/2/23/Rails-5-1-beta1/">Rails 5.1.0.beta1: Loving JavaScript, System Tests, Encrypted Secrets, and more</a></li>
<li><a href="https://speakerdeck.com/claudiob/rails-5-dot-1-upcoming-features">Rails 5.1 Upcoming Features</a></li>
</ul>It’s time for another version of Rails. The first beta of Rails 5.1 was released a few days ago, and it introduces some goodies that will make things a lot easier for people who need to work with JS in their Rails apps. Here’s a quick tour of the new features:Start Using Yarn With Rails Today2017-02-17T13:48:50+00:002017-02-17T13:48:50+00:00https://nithinbekal.com/posts/yarn-rails<p><a href="https://yarnpkg.com/en/">Yarn</a> is a JavaScript package manager,
and a faster alternative to NPM.
It works almost exactly like NPM, but has one advantage -
it creates a <code class="highlighter-rouge">yarn.lock</code> file which you can check into source control,
and make sure you’re running the same version of the packages everywhere.</p>
<p>Rails 5.1 will support Yarn out of the box,
but in the meanwhile, I wanted to try it out on a Rails 5.0 project.
I found the process surprisingly easy and painless.
Let’s see how you can add Bootstrap to an existing Rails project through Yarn.</p>
<p>First of all you need to install Yarn.
On macOS, you can use homebrew to install it using <code class="highlighter-rouge">brew install yarn</code>.
For other OSes, refer to the
<a href="https://yarnpkg.com/lang/en/docs/install/">installation instructions</a>.</p>
<p>Next, run this command to create <code class="highlighter-rouge">package.json</code>.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">yarn init</code></pre></figure>
<p>Next, let’s install <code class="highlighter-rouge">bootstrap</code>:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">yarn add bootstrap</code></pre></figure>
<p>This will install bootstrap to <code class="highlighter-rouge">node_modules/</code>,
and also create <code class="highlighter-rouge">yarn.lock</code> with information about the package versions.
Remember to add <code class="highlighter-rouge">node_modules/</code> to your <code class="highlighter-rouge">.gitignore</code>.</p>
<p>Next, we’ll ask Rails to look for assets in <code class="highlighter-rouge">node_modules</code>
by adding this line to <code class="highlighter-rouge">config/initializers/assets.rb</code>:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="no">Rails</span><span class="p">.</span><span class="nf">application</span><span class="p">.</span><span class="nf">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">paths</span> <span class="o">&lt;&lt;</span> <span class="no">Rails</span><span class="p">.</span><span class="nf">root</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="s1">'node_modules'</span><span class="p">)</span></code></pre></figure>
<p>Tell <code class="highlighter-rouge">application.css</code> and <code class="highlighter-rouge">application.js</code> to load bootstrap’s assets,
by adding these lines:</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># In app/assets/stylesheets/application.css:</span>
<span class="o">*=</span> <span class="nb">require</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">dist</span><span class="o">/</span><span class="n">css</span><span class="o">/</span><span class="n">bootstrap</span>
<span class="c1"># In app/assets/javascripts/application.js:</span>
<span class="sr">//</span><span class="o">=</span> <span class="nb">require</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">dist</span><span class="o">/</span><span class="n">js</span><span class="o">/</span><span class="n">bootstrap</span></code></pre></figure>
<p>And that’s all there is to it.
You can restart your Rails server, and you’ll have bootstrap assets loaded.
This will not longer be necessary once Rails 5.1 ships,
but you can use this to start using Yarn with older Rails versions.</p>
<p>I have <a href="/posts/rails-assets/">previously mentioned Rails Assets</a>,
which is also a great option.
There were <a href="https://github.com/tenex/rails-assets/issues/291">concerns about its future</a>
at one point, but the team have done a great job maintaining it so far.
If you wish to avoid depending on Yarn now, it is a great option.
However, Yarn is set to become a part of Omakase Rails stack,
so it makes sense to start using it while we wait for the new Rails.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://github.com/rails/rails/pull/26836">Add Yarn support in new apps</a></li>
</ul>Yarn is a JavaScript package manager, and a faster alternative to NPM. It works almost exactly like NPM, but has one advantage - it creates a yarn.lock file which you can check into source control, and make sure you’re running the same version of the packages everywhere.Setting Up Jenkins on Ubuntu 16.04 for Ruby on Rails CI2017-02-12T14:50:00+00:002017-02-12T14:50:00+00:00https://nithinbekal.com/posts/jenkins-rails<p>When looking for a hosted continuous integration platform,
one problem is that most services don’t support self hosted repositories.
I was setting up CI for a team that has git repos on self hosted Gitlab.
<a href="https://jenkins.io">Jenkins</a> is a great option for such cases.
In this article we’ll set up the following:</p>
<ul>
<li>Jenkins 2.45 on Ubuntu 16.04</li>
<li>Nginx as reverse proxy for Jenkins</li>
<li>Get an SSL certificate from Letsencrypt</li>
<li>Install Ruby with RVM and Postgres 9.5 for Rails</li>
<li>Run the Rails project’s tests on git push</li>
</ul>
<p>I’m using a <a href="https://m.do.co/c/863244c8a721">Digital Ocean VPS</a> with 2GB RAM for this setup.
Spin up a VPS with Ubuntu 16.04 and
make sure you’re able to SSH as root before continuing.</p>
<h2 id="install-jenkins">Install Jenkins</h2>
<p>First of all, we will use apt-get to install Jenkins.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">wget <span class="nt">-q</span> <span class="nt">-O</span> - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | apt-key add -
<span class="nb">echo </span>deb http://pkg.jenkins-ci.org/debian binary/ <span class="o">&gt;</span> /etc/apt/sources.list.d/jenkins.list
apt update
apt install <span class="nt">-y</span> jenkins</code></pre></figure>
<p>Go to <code class="highlighter-rouge">http://your-ip-address:8080/</code> to get the Jenkins page.
You will be asked for admin password to unlock Jenkins,
which you can get here:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nb">cat</span> /var/lib/jenkins/secrets/initialAdminPassword</code></pre></figure>
<p>Once you have unlocked Jenkins,
install the suggested plugins in the “getting started” wizard,
and then create an admin account.</p>
<h1 id="install-ruby">Install Ruby</h1>
<p>Next, we will set up Ruby 2.3.3 on the server.
Replace 2.3.3 below with whichever version you’re using.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">apt install <span class="nt">-y</span> curl bison build-essential zlib1g-dev libssl-dev <span class="se">\</span>
libreadline5-dev libxml2-dev git-core nodejs
su jenkins <span class="c"># switch to jenkins user</span>
curl <span class="nt">-L</span> https://get.rvm.io | bash <span class="nt">-s</span> stable <span class="nt">--ruby</span><span class="o">=</span>2.3.3
<span class="nb">echo</span> <span class="s1">'source "/var/lib/jenkins/.rvm/scripts/rvm"'</span> <span class="o">&gt;&gt;</span> /var/lib/jenkins/.bashrc
<span class="nb">source</span> ~/.bashrc
rvm install 2.3.3
gem install bundler</code></pre></figure>
<h2 id="install-postgres">Install Postgres</h2>
<p>In this tutorial, we will be installing Postgres 9.5.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">apt install <span class="nt">-y</span> libpq-dev postgresql postgresql-client</code></pre></figure>
<p>Make the following change in the <code class="highlighter-rouge">pg_hba.conf</code> file.
You can find its location by running <code class="highlighter-rouge">find / -name "pg_hba.conf"</code>,
but in our case its most likely location is <code class="highlighter-rouge">/etc/postgresql/9.5/main/pg_hba.conf</code>.</p>
<figure class="highlight"><pre><code class="language-diff" data-lang="diff"><span class="gd">- local all postgres peer
</span><span class="gi">+ local all postgres md5</span></code></pre></figure>
<p>Restart the postgres service with <code class="highlighter-rouge">service postgresql restart</code>.
You might want to set up the database user for your project.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>su - postgres
<span class="nv">$ </span>psql
<span class="nv">postgres</span><span class="o">=</span><span class="c"># create user foobar with password 'mysecretpassword';</span></code></pre></figure>
<h2 id="install-nginx">Install Nginx</h2>
<p>Although we can access Jenkins on port 8080,
it’s always a good idea to enable SSL on a site.
The easiest way to do this is to set up Nginx as a reverse proxy to Jenkins,
and use an SSL certificate from Let’s Encrypt.
Let’s install Nginx:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">apt install nginx</code></pre></figure>
<h2 id="ssl-with-letsencrypt">SSL with Letsencrypt</h2>
<p>We can get a free SSL certificate from <a href="https://letsencrypt.org">Lets Encrypt</a>.
It is as easy as running the following two commands.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>apt install letsencrypt
<span class="nv">$ </span>letsencrypt certonly <span class="nt">--webroot</span> <span class="nt">-w</span> /var/www/html <span class="nt">-d</span> ci.example.com
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/ci.example.com/fullchain.pem. Your
cert will expire on 2017-05-11. To obtain a new version of the
certificate <span class="k">in </span>the future, simply run Let<span class="s1">'s Encrypt again.</span></code></pre></figure>
<p>Here <code class="highlighter-rouge">ci.example.com</code> should be changed to whatever subdomain you’re using.
You can read more about setting up LetsEncrypt SSL certificates
<a href="https://certbot.eff.org/#ubuntuxenial-nginx">here</a>.</p>
<h2 id="configure-nginx">Configure Nginx</h2>
<p>Digital Ocean has an excellent tutorial on
<a href="https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins">setting up Nginx as a reverse proxy for Jenkins</a>.
My Nginx conf is based on that tutorial,
so please refer to that tutorial if you need to learn more about this config.
Change <code class="highlighter-rouge">/etc/nginx/sites-enabled/default</code> to something like below.
Replace <code class="highlighter-rouge">ci.example.com</code> with the subdomain you’ll be using.</p>
<figure class="highlight"><pre><code class="language-nginx" data-lang="nginx"><span class="k">upstream</span> <span class="s">jenkins</span> <span class="p">{</span>
<span class="kn">server</span> <span class="nf">127.0.0.1</span><span class="p">:</span><span class="mi">8080</span> <span class="s">fail_timeout=0</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">server</span> <span class="p">{</span>
<span class="kn">listen</span> <span class="mi">80</span><span class="p">;</span>
<span class="kn">return</span> <span class="mi">301</span> <span class="s">https://</span><span class="nv">$host$request_uri</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">server</span> <span class="p">{</span>
<span class="kn">listen</span> <span class="mi">443</span><span class="p">;</span>
<span class="kn">server_name</span> <span class="s">ci.example.com</span><span class="p">;</span>
<span class="kn">ssl_certificate</span> <span class="n">/etc/letsencrypt/live/ci.example.com/fullchain.pem</span><span class="p">;</span>
<span class="kn">ssl_certificate_key</span> <span class="n">/etc/letsencrypt/live/ci.example.com/privkey.pem</span><span class="p">;</span>
<span class="kn">ssl</span> <span class="no">on</span><span class="p">;</span>
<span class="kn">ssl_session_cache</span> <span class="nf">builtin</span><span class="p">:</span><span class="mi">1000</span> <span class="s">shared:SSL:10m</span><span class="p">;</span>
<span class="kn">ssl_protocols</span> <span class="s">TLSv1</span> <span class="s">TLSv1.1</span> <span class="s">TLSv1.2</span><span class="p">;</span>
<span class="kn">ssl_ciphers</span> <span class="s">HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4</span><span class="p">;</span>
<span class="kn">ssl_prefer_server_ciphers</span> <span class="no">on</span><span class="p">;</span>
<span class="kn">access_log</span> <span class="n">/var/log/nginx/jenkins.access.log</span><span class="p">;</span>
<span class="kn">location</span> <span class="n">/</span> <span class="p">{</span>
<span class="kn">proxy_set_header</span> <span class="s">Host</span> <span class="nv">$host</span><span class="p">;</span>
<span class="kn">proxy_set_header</span> <span class="s">X-Real-IP</span> <span class="nv">$remote_addr</span><span class="p">;</span>
<span class="kn">proxy_set_header</span> <span class="s">X-Forwarded-For</span> <span class="nv">$proxy_add_x_forwarded_for</span><span class="p">;</span>
<span class="kn">proxy_set_header</span> <span class="s">X-Forwarded-Proto</span> <span class="nv">$scheme</span><span class="p">;</span>
<span class="kn">proxy_pass</span> <span class="s">http://jenkins</span><span class="p">;</span>
<span class="kn">proxy_read_timeout</span> <span class="mi">90</span><span class="p">;</span>
<span class="kn">proxy_redirect</span> <span class="s">http://jenkins</span> <span class="s">https://ci.example.com</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="err">}</span></code></pre></figure>
<p>Now you should be able to access Jenkins on the subdomain using https.</p>
<h2 id="add-public-key">Add public key</h2>
<p>Before we set up Jenkins to run our tests,
we need to make sure that it has access to our git repo.
Create an SSH key pair and copy the public key.
You will need to paste it as a deploy key for your project
on Github, Gitlab or whichever host you’re using.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">su jenkins
<span class="nb">cd</span> ~
ssh-keygen <span class="nt">-t</span> rsa <span class="nt">-C</span> <span class="s2">"nithin@example.com"</span></code></pre></figure>
<p>At this point, you should be able to git clone the repo
when logged in as Jenkins.</p>
<h2 id="jenkins-project-configuration">Jenkins project configuration</h2>
<p>Login to Jenkins dashboard and create a new freestyle project.
You will be redirected to the project configuration page.
Set the repository URL in the source code management section.
In the build section, add a new build step and paste the following:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#!/bin/bash</span>
<span class="nb">export </span><span class="nv">RAILS_ENV</span><span class="o">=</span><span class="nb">test
source</span> <span class="nv">$HOME</span>/.bashrc
<span class="nb">cd</span> <span class="nb">.</span> <span class="c"># Force RVM to load the correct Ruby version</span>
bundle install
bundle <span class="nb">exec </span>rails db:create db:schema:load db:migrate
bundle <span class="nb">exec </span>rails <span class="nb">test</span></code></pre></figure>
<p>Save the project, and check that the build is working
by clicking the “build now” link.</p>
<p>Now that you have the build working,
you need to make sure that a build is triggered
whenever someone pushes code or creates a pull request.
Our repo is on a self hosted Gitlab installation,
so I needed to install the Gitlab plugin on Jenkins
and enable the “Build when a change is pushed to GitLab” option.
There are plugins available for other services as well.</p>
<p>One minor drawback of Jenkins is cluttered UI.
<a href="https://jenkins.io/projects/blueocean/">Blue Ocean</a>
is a project aiming to bring a modern UI to Jenkins,
and will eventually replace the existing UI.
I highly recommend trying out the beta version,
which is available as a Jenkins plugin.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://jenkins.io">Jenkins home page</a></li>
<li><a href="https://jenkins.io/projects/blueocean/">Blue Ocean</a> -
Modern UI for Jenkins</li>
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins">How To Configure Nginx with SSL as a Reverse Proxy for Jenkins</a></li>
</ul>When looking for a hosted continuous integration platform, one problem is that most services don’t support self hosted repositories. I was setting up CI for a team that has git repos on self hosted Gitlab. Jenkins is a great option for such cases. In this article we’ll set up the following:Creating and Applying Git Patch Files2017-02-12T11:25:21+00:002017-02-12T11:25:21+00:00https://nithinbekal.com/posts/git-patch<p>I was recently reviewing code on a project where I didn’t have write access,
and wanted to suggest a small code change to the team.
As I couldn’t send a pull request to make the change,
I had to look into how to create a patch that I could then email.</p>
<h2 id="creating-a-patch">Creating a patch</h2>
<p>In this example, we will add a line to a Rails project’s <code class="highlighter-rouge">Gemfile</code>.
When we add a <code class="highlighter-rouge">gem 'rspec-rails'</code> line, <code class="highlighter-rouge">git diff</code> looks like this:</p>
<figure class="highlight"><pre><code class="language-diff" data-lang="diff"><span class="gh">diff --git a/Gemfile b/Gemfile
index c661619..989efe8 100644
</span><span class="gd">--- a/Gemfile
</span><span class="gi">+++ b/Gemfile
</span><span class="gu">@@ -24,6 +24,7 @@ group :development do
</span> gem 'listen', '~&gt; 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~&gt; 2.0.0'
<span class="gi">+ gem 'rspec-rails'
</span> end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]</code></pre></figure>
<p>We can save this as a patch file, without committing the code.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">git diff <span class="o">&gt;</span> add-rspec.patch</code></pre></figure>
<p>Now let’s look at what happens when you add a new file to the working directory.
Create a <code class="highlighter-rouge">notes.txt</code>, add a couple of lines there, and run <code class="highlighter-rouge">git diff</code>.
You will notice that the new file is not present in the diff.
To get it, you will need to stage all the files and then run <code class="highlighter-rouge">git diff --cached</code>.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">git add <span class="nb">.</span>
git diff <span class="nt">--cached</span> <span class="o">&gt;</span> add-rspec.patch</code></pre></figure>
<p>Now the patch looks like:</p>
<figure class="highlight"><pre><code class="language-diff" data-lang="diff"><span class="gh">diff --git a/Gemfile b/Gemfile
index c661619..989efe8 100644
</span><span class="gd">--- a/Gemfile
</span><span class="gi">+++ b/Gemfile
</span><span class="gu">@@ -24,6 +24,7 @@ group :development do
</span> gem 'listen', '~&gt; 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~&gt; 2.0.0'
<span class="gi">+ gem 'rspec-rails'
</span> end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
<span class="gh">diff --git a/notes.txt b/notes.txt
</span>new file mode 100644
<span class="gh">index 0000000..3a5e395
</span><span class="gd">--- /dev/null
</span><span class="gi">+++ b/notes.txt
</span><span class="gu">@@ -0,0 +1,2 @@
</span><span class="gi">+We are using rspec for testing.
+Rspec is the best.</span></code></pre></figure>
<h2 id="binary-patches">Binary patches</h2>
<p>What if the new file we added was a binary file?
This time we will add a <code class="highlighter-rouge">logo.jpeg</code> file and stage it.
We can use the <code class="highlighter-rouge">--binary</code> option.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">git add <span class="nb">.</span>
git diff <span class="nt">--staged</span> <span class="nt">--binary</span> <span class="o">&gt;</span> mypatch.patch</code></pre></figure>
<p>The patch file will look like this:</p>
<figure class="highlight"><pre><code class="language-diff" data-lang="diff"><span class="gh">diff --git a/Gemfile b/Gemfile
index c661619..989efe8 100644
</span><span class="gd">--- a/Gemfile
</span><span class="gi">+++ b/Gemfile
</span><span class="gu">@@ -24,6 +24,7 @@ group :development do
</span> gem 'listen', '~&gt; 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~&gt; 2.0.0'
<span class="gi">+ gem 'rspec-rails'
</span> end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
<span class="gh">diff --git a/app/assets/images/logo.jpeg b/app/assets/images/logo.jpeg
</span>new file mode 100644
<span class="gh">index 0000000000000000000000000000000000000000..064fa38be3ecd426a3c8977ed43df627c6f6f229
</span>GIT binary patch
literal 50966
zcmbT6RZtv2x1a}i2o6CLAUMHc&amp;=7(LcbCE49fAa#;4l!}9R?j7La@Qz-3jh4+5Goz
z?N;s6?w*#Hmg=v%T8{i(_`3mkBQGr{4M0Eu01*Bqz~5JZu%w!lgod)35UrItyREZ@
...a few hundred lines like this...
z_i{8b#lS&gt;d00-gE&lt;y-oK1ih6&amp;zzl1$iJXXs289`1p0s}urC+&amp;SB#PYPr9*_xMg=~A
v97b_p1peNmKNs|WZ3Z9xD7de_7qxsD`+5QY01Jai0{XNQKl(o6fIt7)0U9~_
literal 0
HcmV?d00001</code></pre></figure>
<h2 id="creating-patches-from-commits">Creating patches from commits</h2>
<p>Suppose you have created a new branch and made two commits -
one for adding rspec to Gemfile, and another for the notes file.
You can create a patch file for the commits by using <code class="highlighter-rouge">git format-patch</code>.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>git format-patch master
0001-Add-rspec-to-gemfile.patch
0002-Add-notes-file.patch</code></pre></figure>
<p>This will create a patch file for each commit.
Let’s take a look at what one of the files looks like:</p>
<figure class="highlight"><pre><code class="language-diff" data-lang="diff">From 57c9101b014623049a6bb75ee43505058d494077 Mon Sep 17 00:00:00 2001
From: Nithin Bekal &lt;nithin@example.com&gt;
Date: Sun, 12 Feb 2017 13:31:03 +0530
Subject: [PATCH 1/2] Add rspec to gemfile
---
Gemfile | 1 +
1 file changed, 1 insertion(+)
<span class="gh">diff --git a/Gemfile b/Gemfile
index c661619..989efe8 100644
</span><span class="gd">--- a/Gemfile
</span><span class="gi">+++ b/Gemfile
</span><span class="gu">@@ -24,6 +24,7 @@ group :development do
</span> gem 'listen', '~&gt; 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~&gt; 2.0.0'
<span class="gi">+ gem 'rspec-rails'
</span> end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
<span class="gd">--
</span>2.11.1</code></pre></figure>
<p>If you don’t want to create multiple patch files, you can do this:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">git format-patch master <span class="nt">--stdout</span> <span class="o">&gt;</span> rspec-changes.patch</code></pre></figure>
<h2 id="applying-a-patch">Applying a patch</h2>
<p>Now that we have looked at the different ways to create patches,
let’s see how we can apply a patch file to the working directory.
Let’s checkout a <code class="highlighter-rouge">review-rspec-patch</code> branch from master first.
Now let’s apply one of the patch files we created earlier:</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">git apply 0001-Add-rspec-to-gemfile.patch</code></pre></figure>
<p>Using <code class="highlighter-rouge">git apply</code> provides the patch as unstaged changes in your branch.
If you want to apply the patches as commits, you can use <code class="highlighter-rouge">git am</code>.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>git am rspec-changes.patch
Applying: Add rspec to gemfile
Applying: Add notes file
<span class="nv">$ </span>git log <span class="nt">--oneline</span>
ac9caff Add notes file
f784b22 Add rspec to gemfile
8619310 ...older commits...</code></pre></figure>
<h2 id="links">Links</h2>
<ul>
<li><a href="http://rypress.com/tutorials/git/patch-workflows">Patch Workflows - Ry’s Git Tutorial</a></li>
<li><a href="https://robots.thoughtbot.com/send-a-patch-to-someone-using-git-format-patch">Send A Patch To Someone Using <code class="highlighter-rouge">git format-patch</code></a>:
Thoughtbot Blog</li>
</ul>I was recently reviewing code on a project where I didn’t have write access, and wanted to suggest a small code change to the team. As I couldn’t send a pull request to make the change, I had to look into how to create a patch that I could then email.Favorite Books of 20162017-02-09T13:44:08+00:002017-02-09T13:44:08+00:00https://nithinbekal.com/posts/favorite-books-2016<p>41 books and over 13,000 pages.
Despite reading quite a few books in 2016,
this list will be much shorter than the one from
<a href="/posts/favorite-books-2015/">last year</a>.</p>
<h2 id="non-fiction">Non fiction</h2>
<p><strong>1. <a href="http://amzn.to/2kHyGq7">What If: Serious Scientific Answers to Absurd Hypothetical Questions</a> (Randall Munroe)</strong></p>
<p>As a huge fan of the XKCD comics I’ve always wished for a prose version of XKCD.
This collection of weird “what if” questions are exactly what I was hoping for.
The amount of thought that Munroe has put into questions like
“what would happen if you hit a baseball at 90% speed of light?”
is simply incredible.
One of the funniest books and most entertaining science books I’ve come across.
It had me laughing out loud every few pages.</p>
<p><strong>2. <a href="http://amzn.to/2lkoXcz">Cosmos</a> (Carl Sagan)</strong></p>
<p>I wish I had read this sooner.
There are few books that have influenced
a generation of science enthusiasts the way Cosmos has done.
On the surface, it is a history of our Universe,
but it also contains Sagan’s reflections on our place in it.
This goes straight into my list of favorite science books.</p>
<p><strong>3. <a href="http://amzn.to/2kHDX16">The Ascent of Money</a> (Niall Ferguson)</strong></p>
<p>An excellent history of financial systems, starting with clay tablets in ancient Mesopotamia
all the way through currency, stocks, bonds and the housing bubble of 2008.
There are places where I found the technical details hard to follow,
and wished there was more explanation of the jargon.
But it’s an excellent read about how the evolution of finance has affected history.</p>
<p><strong>4. <a href="http://amzn.to/2kHLk8H">The Sceptical Patriot</a> (Sidin Vadukut)</strong></p>
<p>Fact checks some common “email forward facts” about India,
like Susruta being the world’s first plastic surgeon, or the invention of zero,
or Sanskrit being the most suitable language for computers.
Each chapter walks through the history behind one such claim.
Many of these “facts” are indeed factual,
in case you thought this was all about disproving them.</p>
<p>Vadukut is right at home in the pop history genre.
I used to read his blog years ago,
and it’s nice to see him turn to writing a non-fiction book.
His writing is engaging and funny,
and I find his non fiction a lot more fun than his novels.</p>
<h2 id="programming">Programming</h2>
<p><strong>1. <a href="http://amzn.to/2krKC0V">Elixir in Action</a> (Sasa Juric)</strong></p>
<p>Elixir has two awesome introductory books -
this one and Dave Thomas’ <a href="http://amzn.to/2kLSNDy">Programming Elixir</a>.
Dave’s book focused more on the language,
while this one walks through building an app
using the power of Elixir and OTP,
and then looking into scaling and distribution.
This book is more practical,
although it skips on some of the language features (macros, testing, etc)
that are covered in Dave Thomas’ book.</p>
<p><strong>2. <a href="http://amzn.to/2kmo3YP">Programming Phoenix</a> (Chris McCord, Bruce Tate, Jose Valim)</strong></p>
<p>Not only is this an excellent introduction to Phoenix,
but also helped clarify some features of Elixir,
like protocols, that I hadn’t understood before.
Most books about web frameworks become outdated before they are even published.
But this one does a great deal to explain how to use OTP
to build concurrent, scalable systems with Phoenix,
and that will keep the book relevant for quite a while.
The second part of the book, which covers the realtime features of Phoenix,
is something I expect to return to many times in the future.</p>
<p><strong>3. <a href="http://amzn.to/2krzKQs">Test-Driven Development, By Example</a> (Kent Beck)</strong></p>
<p>Fantastic introduction to TDD.
The examples are such that they are great
for learning about good design principles as well.
Not too surprising since TDD is all about achieving good design.
A must-read for every software developer.</p>
<h2 id="fiction">Fiction</h2>
<p><strong>1. <a href="http://amzn.to/2krxpVY">New Spring</a> (Robert Jordan)</strong></p>
<p>A final chance to revisit the Wheel of Time characters.
Four years, 15 books and 12,000-odd pages after starting the series, I’m finally done.
Although there are individual books that are disappointing,
this series as a whole is one of the best in the fantasy genre.</p>
<p>This prequel is mostly background on 3 major characters in the main series - Siuan, Moirane and Lan.
The plot moves glacially for the first three-quarters of the book
and then picks up at the very end.
I learned a few things about the Wheel of Time world
that I hadn’t noticed in the other books.
Definitely worth a read if you liked the series.</p>
<p><strong>2. <a href="http://amzn.to/2lktZpu">Brave New World</a> (Aldous Huxley)</strong></p>
<p>It’s difficult to talk about this book without mentioning <a href="http://amzn.to/2k830bO">1984</a>.
They represent two kinds of totalitarianisms -
Orwell’s is one driven by fear, while Huxley’s is driven by conformity,
and we see a bit of both in the world around us.
Like 1984, Brave New World is a book worth reading,
because it makes you think deeply about what kind of world you’re living in.</p>
<p><strong>3. <a href="http://amzn.to/2krOLBV">Ender’s Game</a> (Orson Scott Card)</strong></p>
<p>The book has its flaws -
the characters aren’t entirely 3 dimensional,
the plot has far too many holes in it
and has all the subtlety of a battering ram,
and there’s a whole unnecessary chapter at the end
that just sets up the sequel.
But it’s a damn good work of military scifi.
It got me hooked right away,
despite already knowing the plot through the movie.</p>
<h2 id="2017">2017</h2>
<p>I’ve started reading Patrick Rothfuss’ <a href="http://amzn.to/2kSWeek">Name of the Wind</a>,
and hope to read one of Brandon Sanderson’s <a href="http://amzn.to/2kSXhe4">Mistborn</a> books this year.
I have 44 unread books now, 30 of them ebooks.
Those should keep me busy through 2017.</p>
<h2 id="links">Links</h2>
<ul>
<li>Previous lists:
<a href="/posts/favorite-books-2015/">2015</a>,
<a href="/posts/favorite-books-2014/">2014</a>,
<a href="/posts/favorite-books-2013/">2013</a></li>
<li><a href="/notes/books/">Other books lists</a></li>
</ul>41 books and over 13,000 pages. Despite reading quite a few books in 2016, this list will be much shorter than the one from last year.