<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prasanna Natarajan</title>
    <description>The latest articles on DEV Community by Prasanna Natarajan (@npras).</description>
    <link>https://dev.to/npras</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94913%2F0ce84e52-e314-4570-996b-cd43b2f65f4a.jpeg</url>
      <title>DEV Community: Prasanna Natarajan</title>
      <link>https://dev.to/npras</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/npras"/>
    <language>en</language>
    <item>
      <title>Scala vs Ruby - Creating a while loop DSL</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Sat, 15 Feb 2020 12:22:01 +0000</pubDate>
      <link>https://dev.to/npras/scala-vs-ruby-creating-a-while-loop-dsl-29e4</link>
      <guid>https://dev.to/npras/scala-vs-ruby-creating-a-while-loop-dsl-29e4</guid>
      <description>&lt;p&gt;I'm learning Scala. It's a powerful language with lots of features. One such thing is called the &lt;a href="https://docs.scala-lang.org/tour/by-name-parameters.html"&gt;by-name parameters&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In most languages, a while loop will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;# statements&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ruby implementation of this would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Define the method first&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
    &lt;span class="n"&gt;my_while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;cond&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# and then call it&lt;/span&gt;
&lt;span class="n"&gt;my_while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"i: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the best I could do. The &lt;code&gt;condition&lt;/code&gt; has to be declared explicitly as an anonymous function (&lt;a href="https://npras.in/tech/ruby-blocks-with-js"&gt;proc/lambda/block whatever!&lt;/a&gt;). It doesn't quite give you the native &lt;code&gt;while&lt;/code&gt; look.&lt;/p&gt;

&lt;p&gt;But in Scala we could have a function that looks exactly like the native &lt;code&gt;while&lt;/code&gt;. Here's how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Define the method first&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt;
    &lt;span class="nf"&gt;my_while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;// and then call it.&lt;/span&gt;
&lt;span class="nf"&gt;my_while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"j: ${j}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is because the first argument to the function - &lt;code&gt;cond&lt;/code&gt; - is declared as a by-name parameter. This means the compiler will treat it as an anonymous function. And then whenever the &lt;code&gt;cond&lt;/code&gt; is referred in the function definition, this anonymous function will be executed and it's return value be put in its place.&lt;/p&gt;

&lt;p&gt;The tradeoff here is, when someone looks at the calling code, especially the condition &lt;code&gt;j &amp;lt; 10&lt;/code&gt;, they might think of it as a simple boolean expression and think that it's evaluated immediately on first call. It might trip them. They'll have to see the method definition to really know that it's a by-name parameter instead of a normal boolean one.&lt;/p&gt;

&lt;p&gt;With Ruby, the tradeoff is that the calling code doesn't look like the native dsl. But the great part is that anyone coming across the calling code anywhere will know exactly what the method is going to do with the anon function that was passed.&lt;/p&gt;

&lt;p&gt;I prefer the Ruby way, but was really impressed that Scala could do it exactly as per the DSL.&lt;/p&gt;

</description>
      <category>scala</category>
      <category>ruby</category>
      <category>dsl</category>
    </item>
    <item>
      <title>Building RSS feeds for static-sites built with nanoc</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Sun, 20 Oct 2019 17:04:22 +0000</pubDate>
      <link>https://dev.to/npras/building-rss-feeds-for-static-sites-built-with-nanoc-2ihm</link>
      <guid>https://dev.to/npras/building-rss-feeds-for-static-sites-built-with-nanoc-2ihm</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/npras/ruby-developers-try-nanoc-for-your-static-sites-it-s-great-40g4"&gt;last post&lt;/a&gt;, I mentioned about why Nanoc is a great choice for building your little static site if you love Ruby.&lt;/p&gt;

&lt;p&gt;There I mentioned I have 3 separate blogs in a single site, in single domain. If you want your readers to get notified of your blog posts written in a specific blog, and if you never heard about email, and if you are an yesteryear geek, then adding RSS feeds to your blogs is a great idea.&lt;/p&gt;

&lt;p&gt;With RSS feeds (or atom feeds), your readers can subscribe to your blog using any feed reader tool (like Feedly, Newsblur or &lt;del&gt;Google Reader&lt;/del&gt;). Now they don't have to refresh their screen in hopes of dying to read your latest blog post. The feed reader tool will pull the latest posts automatically and they'll be able to read it as soon as a new post is published.&lt;/p&gt;

&lt;p&gt;Anyway, with nanoc, you have to build everything from scratch. But to make things a bit easier, they have a special &lt;a href="https://nanoc.ws/doc/reference/helpers/"&gt;blogging helper&lt;/a&gt; module. It's in-built and comes with the necessary functions to create a blog. Once such thing is the &lt;code&gt;atom_feed()&lt;/code&gt; function. Just calling it from the right place with right arguments will give your site a nice rss feed.&lt;/p&gt;

&lt;p&gt;Let's see how to create these in your nanoc blog, and especially how to do so if you have multiple blogs in same site like I do.&lt;/p&gt;

&lt;p&gt;The first step is deciding how you want the feed url to look like? Are you ok with &lt;code&gt;example.com/subscribe&lt;/code&gt;, &lt;code&gt;example.com/feed&lt;/code&gt; or &lt;code&gt;example.com/feed.xml&lt;/code&gt; etc. This route is going to be defined in the &lt;code&gt;Rules&lt;/code&gt; file in nanoc.&lt;/p&gt;

&lt;p&gt;Mine looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sx"&gt;%w(tech books general)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# atom feeds for specific blogs&lt;/span&gt;
  &lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s2"&gt;"/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/feed.erb"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="ss"&gt;:erb&lt;/span&gt;
    &lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="s2"&gt;"/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/feed.xml"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This creates 3 feed urls for my 3 blogs. The &lt;code&gt;compile&lt;/code&gt; method is going to look for the &lt;code&gt;/tech/feed.erb&lt;/code&gt; file inside the &lt;code&gt;content/tech/&lt;/code&gt; folder. And similarly for the other 2 blogs. It then uses the erb filter to make sense of these erb files. And finally, it makes this path available for the site at &lt;code&gt;/tech/feed.xml&lt;/code&gt; (Eg: &lt;a href="https://npras.in/tech/feed.xml"&gt;https://npras.in/tech/feed.xml&lt;/a&gt;) Now anyone with this url can subscribe specifically to this tech blog.&lt;/p&gt;

&lt;p&gt;Now the second part. The url should return the correct feed xml once it is visited. That's what'll make it work. We can use the &lt;code&gt;atom_feed()&lt;/code&gt; function that nonc gives us for this. Put this method with the right arguments in a &lt;code&gt;feed.erb&lt;/code&gt; file in this path: &lt;code&gt;/content/tech/feed.erb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;atom_feed&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;articles: &lt;/span&gt;&lt;span class="n"&gt;sorted_articles_of_kind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="s2"&gt;"Prasanna Natarajan's tech blog"&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;

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



&lt;p&gt;That's it. Now, when you visit that xml url, you'll have your feed returned. You can use it to subscribe to the blog from anywhere.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;sorted_articles_of_kind&lt;/code&gt; is a helper method in my &lt;code&gt;lib/helpers.rb&lt;/code&gt; file. The function looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sorted_articles_of_kind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;sorted_articles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_all&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;=~&lt;/span&gt; &lt;span class="sr"&gt;%r{/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  One feed to rule them all
&lt;/h4&gt;

&lt;p&gt;I also have a common feed that has contents from all 3 blogs. This is for anyone who wants to know about the latest post coming across all 3 blog. &lt;a href="https://npras.in/feed.xml"&gt;https://npras.in/feed.xml&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To create this, first create a compile entry in the &lt;code&gt;content/feed.erb&lt;/code&gt; file. And then add it with this content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;atom_feed&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;articles: &lt;/span&gt;&lt;span class="n"&gt;sorted_articles&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you know how to setup rss/atom feeds for sites using nanoc. Happy blogging!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>nanoc</category>
    </item>
    <item>
      <title>Ruby developers! Try Nanoc for your static sites. It's great.</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Sat, 19 Oct 2019 13:57:02 +0000</pubDate>
      <link>https://dev.to/npras/ruby-developers-try-nanoc-for-your-static-sites-it-s-great-40g4</link>
      <guid>https://dev.to/npras/ruby-developers-try-nanoc-for-your-static-sites-it-s-great-40g4</guid>
      <description>&lt;p&gt;&lt;a href="https://nanoc.ws/"&gt;Nanoc&lt;/a&gt; is a static site generator just like &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt; and &lt;a href="https://gohugo.io/"&gt;Hugo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For my site, after trying Jekyll, Hugo, Octopress and Wordpress, I've currently (hopefully finally) settled into nanoc &lt;strong&gt;for these specific reasons&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It uses &lt;code&gt;erb&lt;/code&gt; as the templating language. As a ruby developer, this is a boon for me. There's nothing new to learn here as it's just ruby inside of the &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt; tags. I missed this sorely when I built my site with Jekyll which uses Liquid template and Hugo which uses Go's template which I know nothing of. (And I'm not going to spend time learning either of which in detail because all I want is to write some content in a site tweaked to my needs.)&lt;/p&gt;

&lt;p&gt;It allows to have multiple blogs in the same site. I don't know why most people don't need this. It's an awesome feature to have in a personal site.&lt;br&gt;
Putting categories for each in a same blog is not a solution I like. I like to have them separated out. With their own archives page and rss feeds. Nanoc allows me to build separate feeds for these blogs, and also an all-together-in-one-bundle feed as well.&lt;br&gt;
I have 3 blogs in my site right now, all catering to a different kind of audience.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://npras.in/tech/"&gt;tech blog&lt;/a&gt; is where I write about programming stuff. I want to showcase this to my prospective employers. It's also a reference to myself in future about things I'm learning now.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://npras.in/general/"&gt;general blog&lt;/a&gt; is where I write about everything else. I share it with my friends. Anything goes here.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://npras.in/books"&gt;book notes&lt;/a&gt; is where I write reviews of books I've read and have kindle highlights extracted and put on there. I was inspired by Derek Sivers' &lt;a href="https://sivers.org/book"&gt;book notes&lt;/a&gt;. I share these posts only with friends who read books.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I host my site in github pages. So a git push is enough to deploy the site. But normally the nanoc repo and the output folder having the static site would have to be pushed separately. But with nanoc's &lt;a href="https://nanoc.ws/doc/deploying/"&gt;git setup&lt;/a&gt;, I just run &lt;code&gt;nanoc deploy&lt;/code&gt; and it will create an automatic commit out of the newly built/changed files and then push it too. So the 2 steps are wrapped up in a single deploy step.&lt;/p&gt;

&lt;p&gt;Having said that, I'll list some of the problems I faced with nanoc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's strength is also its weakness. It allows me to build any sort of a blog, which just means I have to sweat out the details of how exactly to bend nanoc to get my needs met. My &lt;code&gt;Rules&lt;/code&gt; file took quite some to come up with.&lt;/li&gt;
&lt;li&gt;I had trouble setting up syntax highlighting. I love to have monokai. But even after following the documentation, I couldn't get it working per my wish. It could be my bad css skills too. But I removed it completely and now rely upon some basic css to highlight code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want more details on how to setup the deployment, the &lt;code&gt;Rules&lt;/code&gt; file, multiple blogs and their feeds, checkout the &lt;a href="https://github.com/npras/npras.in.nanoc"&gt;source code&lt;/a&gt; at github.&lt;/p&gt;

&lt;p&gt;It's a great little tool that assumes nothings and gives you flexibility to build your site to your needs.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails testing tip: Remember to revert your in-memory configuration setting changes in each testcase</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Tue, 08 Oct 2019 14:32:27 +0000</pubDate>
      <link>https://dev.to/npras/rails-testing-tip-remember-to-revert-your-in-memory-configuration-setting-changes-in-each-testcase-4ioe</link>
      <guid>https://dev.to/npras/rails-testing-tip-remember-to-revert-your-in-memory-configuration-setting-changes-in-each-testcase-4ioe</guid>
      <description>&lt;p&gt;In Rails, each test case is wrapped in a database transaction. With both minitest and rspec. Once the test case is finished running, the transaction rolls back all the database-related changes. This means all of our app's testcases run with the exact same snapshot of the database. This ensures the database doesn't introduce any inconsistencies.&lt;/p&gt;

&lt;p&gt;But what about the configuration settings that our app relies upon? We might write testcases that explicitly test the app's behavior due to the changes in one of these configuration settings. So if we change a value in one testcase, then it is your responsibility to restore it at the end of the testcase to it's previous value. The database framework can't help!&lt;/p&gt;

&lt;p&gt;If you don't explicitly revert these config settings, then they'll come back to bite you later in the form of some testcase failure. It will perplex you. It did me.&lt;/p&gt;

&lt;p&gt;So what's the solution?&lt;/p&gt;

&lt;p&gt;I came across a good pattern in the &lt;a href="https://github.com/plataformatec/devise"&gt;Devise gem's&lt;/a&gt; source code. The idea is simple. They make the config setting change in a test helper method and yield the testing code to this helper method. One the block is run, the config setting is reverted to its old value (which was saved in the beginning). The code can explain better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="c1"&gt;# Execute the block setting the given values and restoring old values after&lt;/span&gt;
  &lt;span class="c1"&gt;# the block is executed.&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;old_values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;new_values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;old_values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
      &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt; &lt;span class="ss"&gt;:"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="ss"&gt;="&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
  &lt;span class="k"&gt;ensure&lt;/span&gt;
    &lt;span class="n"&gt;old_values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt; &lt;span class="ss"&gt;:"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="ss"&gt;="&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And used like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="s1"&gt;'with sign_out_all_scopes as false'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;swap&lt;/span&gt; &lt;span class="no"&gt;Devise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;sign_out_all_scopes: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="c1"&gt;# test code&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What happens here? The default value for &lt;code&gt;Devise.sign_out_all_scopes&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;. But in this testcase, we need to test a scenario with this value set to &lt;code&gt;false&lt;/code&gt;. The swap method takes care of saving the current value of this attribute before changing it to the new value we want it to hold. And then it yields a block which is where we can write our test code. Once it is run, the swap method restores the saved old value to the attribute in the ensure block.&lt;/p&gt;

&lt;p&gt;This can apply to any kind of in-memory configuration object that allows both reading and writing. Make use of this tactic to avoid future headaches!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>testing</category>
    </item>
    <item>
      <title>I'm a MonkeyPatch Debugger</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Tue, 24 Sep 2019 06:00:14 +0000</pubDate>
      <link>https://dev.to/npras/use-this-trick-to-test-your-rails-patch-quickly-in-production-without-yet-publishing-it-33io</link>
      <guid>https://dev.to/npras/use-this-trick-to-test-your-rails-patch-quickly-in-production-without-yet-publishing-it-33io</guid>
      <description>&lt;p&gt;In startups the deployment cycle will be faster compared to big and old companies. It takes time for your fix to go live in production. So any small mis-considerations are going to be costly.&lt;/p&gt;

&lt;p&gt;If you have access to production or staging environment, and if you are doing it carefully, then this trick will save you time and bring in new insights to add into the patch.&lt;/p&gt;

&lt;p&gt;It's simple. Often your fix might be a change in a method or a class or a module. Maybe you added new lines/methods or removed some. &lt;strong&gt;In a rails console, you could open that class/module and then make your change and test it right within the console.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example.&lt;/p&gt;

&lt;p&gt;Let's say you have a class &lt;code&gt;User&lt;/code&gt;. The fix for the current bug (or maybe it's just a feature addition, not a bug) might be to add a &lt;code&gt;full_name&lt;/code&gt; method to it.&lt;/p&gt;

&lt;p&gt;Locally, you can make that change in your &lt;code&gt;user.rb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
  &lt;span class="c1"&gt;# 100s of lines of other methods&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;full_name&lt;/span&gt;
    &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then simply copy paste this whole class into the production &lt;code&gt;rails console&lt;/code&gt; environment. Now, within there, you have access to this method. You can see if it works as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Patrick'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Jane'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;full_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You don't even have to paste the whole big class. In a separate file, you can "open" the User class and just add the method. Due to ruby's "open classes" functionality, this method will get added to the existing class.&lt;/p&gt;

&lt;p&gt;Note that this won't affect the existing rails server processes that might be still running in that server. Those processes aren't aware of this change. This is only in the context of the &lt;code&gt;rails console&lt;/code&gt; and will die as soon as you exit it.&lt;/p&gt;

&lt;p&gt;Now, why would you want to do it? Fair question. A sane person would do it locally, write tests to verify his assumptions and also to test his fix.&lt;/p&gt;

&lt;p&gt;But sometimes it is hard to setup all the required data locally. Fixtures and factories might not have been there already for you to easily pick and demonstrate the bug and the fix. It takes time to setup data for that special weird unique case you are dealing right now.&lt;/p&gt;

&lt;p&gt;On the other hand, production has all the data you want and they are also real! So you get to play with real data when you patch the class.&lt;/p&gt;

&lt;p&gt;With the newly found confidence that your quick fix works, you can now go back to your dev environment, add the fix, write tests and push through the regular deployment pipeline.&lt;/p&gt;

&lt;p&gt;I know what you are thinking. &lt;strong&gt;"This is dangerous advice".&lt;/strong&gt; It is indeed. But only if you don't know what you are doing. I generally &lt;strong&gt;don't&lt;/strong&gt; do this if the fix changes the state of the application. That is, if it makes permanent changes to the system, like any database inserts/updates/deletes or cache system updates etc. I only do this if all I need is to read some data from database. No harm done to anyone or anything this way.&lt;/p&gt;

&lt;p&gt;There are so many wonderful ways to debug a ruby application: pry, byebug, redmine's breakpoints, &lt;a href="https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html"&gt;puts&lt;/a&gt;, tracepoint, profiles, benchmarking etc. But I've never seen anyone write about this method, probably because it's silly (and also dangerous). But I've found it to be helpful in many occasions to quickly touch and feel the pulse of a fix.&lt;/p&gt;

&lt;p&gt;Try it the next time. Safely though!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Javascript Inheritance explained in plain English (aka Ruby)</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Thu, 19 Sep 2019 15:59:22 +0000</pubDate>
      <link>https://dev.to/npras/javascript-inheritance-explained-in-plain-english-aka-ruby-5218</link>
      <guid>https://dev.to/npras/javascript-inheritance-explained-in-plain-english-aka-ruby-5218</guid>
      <description>&lt;p&gt;Ok, don't get mad at the title and leave too soon. I put it for 2 reasons: My programming mother-tongue is ruby. I interpret other language features by comparing it with ruby. And Ruby really does feels like English 🤷‍♂️. Even if you don't know/care about ruby, you might still benefit about the inheritance knowledge.&lt;/p&gt;

&lt;p&gt;I wrote this post long ago when I want to understand Javascript basics. There's also a companion post about &lt;a href="https://dev.to/npras/a-primer-about-javascript-s-prototype-1g85"&gt;Javascript Prototype&lt;/a&gt;. You might want to check that too to get a better undersanding of the basics.&lt;/p&gt;

&lt;p&gt;So, here we go.&lt;/p&gt;

&lt;p&gt;In Ruby we have first-class syntax support to do almost anything required to do Object Oriented Programming, or even any other facets of programming techniques. We have procs, lambdas, inheritance, ability to include or extend a module, class and object concepts etc. That's why it is attractive as we have concise syntax for almost everything we'd ever want.&lt;/p&gt;

&lt;p&gt;But in Javascript there are only a very few of these. &lt;del&gt;No special syntax support for defining classes, and no straight-forward inheritance support. All it has is these:&lt;/del&gt; a well defined object and function entities, and infallible concepts like prototype, object binding, scopes and contexts.&lt;/p&gt;

&lt;p&gt;(2019 Update: We do have great support for classes and inheritance in JS now. But it's just &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;syntax sugar&lt;/a&gt;. The underlying implementation is based upon functions and prototypes. So understanding this will make you cool.)&lt;/p&gt;

&lt;p&gt;However, with these minimal capabilities, and with a thorough grasp of the language's strengths and weaknesses, you can do almost anything with Javascript. In the face of emerging front-end frameworks and NodeJS, it's high time you get involved in understanding and mastering the Javascript. In this post, we'll see how we can achieve inheritance in Javascript by juxtaposing inheritance from Ruby.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inheritance
&lt;/h3&gt;

&lt;p&gt;What is inheritance in Object Oriented Programming? I can come up with 3 minimal tests to decide if inheritance is implemented or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A Subtype object should be and instance of both the Subtype and the Supertype from which Subtype inherits.&lt;/li&gt;
&lt;li&gt;  The Subtype object should inherit properties from the Supertype definition.&lt;/li&gt;
&lt;li&gt;  Subtype should be able to override properties defined in Supertype.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll see examples of these ideas using Ruby.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ruby's Inheritance
&lt;/h3&gt;

&lt;p&gt;Consider a car object which is of a specific make - Hyundai I20Asta. It can have make-specific properties like steering_type, engine_type, proprietary fuel saving technology etc. But at its core, it's simply a car that has all the general attributes of a car like the number of wheels, transmission technique, engine type etc. So we can inherit an I20Asta object from a generic Car object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;available_fuel_options&lt;/span&gt;
    &lt;span class="sx"&gt;%w(petrol diesel lpg)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;steering_type&lt;/span&gt;
    &lt;span class="s1"&gt;'manual'&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;I20Asta&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Car&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:owner&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;steering_type&lt;/span&gt;
    &lt;span class="s1"&gt;'power'&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With separate objects for both a car and an i20asta car, we can test the 3 inheritance ideas described above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;a_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;john_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 1. subtype should be instance of supertype&lt;/span&gt;
&lt;span class="n"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt; &lt;span class="no"&gt;I20Asta&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;span class="n"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt; &lt;span class="no"&gt;Car&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;

&lt;span class="c1"&gt;# 2. subtype should inherit properties from supertype&lt;/span&gt;
&lt;span class="n"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;available_fuel_options&lt;/span&gt;
&lt;span class="c1"&gt;# ['pertrol', 'diesel', 'lpg']&lt;/span&gt;
&lt;span class="c1"&gt;# Note that the I20Asta class doesn't define or override the available_fuel_options method.&lt;/span&gt;

&lt;span class="c1"&gt;# 3. subtype should be able to override properties defined in supertype&lt;/span&gt;
&lt;span class="n"&gt;a_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;steering_type&lt;/span&gt; &lt;span class="c1"&gt;# manual&lt;/span&gt;
&lt;span class="n"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;steering_type&lt;/span&gt; &lt;span class="c1"&gt;# power&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's see how to do the same in Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript's Inheritance
&lt;/h3&gt;

&lt;p&gt;Let's first create the constructor functions for both Car and I20Asta. Objects will be created only from these constructors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;front&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;back&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;steering_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;manual&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;steering_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;power&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(Instead of adding properties to the constructor functions directly, we've added them in the function's prototype object. This way, the properties are shared by all of the objects created from these functions instead of occupying separate space in memory.)&lt;/p&gt;

&lt;p&gt;Note that we haven't yet implemented inheritance yet. There will be no association of any kind between objects created from these functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false. Inheritance not yet implemented.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  An aside about Prototype Object
&lt;/h4&gt;

&lt;p&gt;When we ask a Javascript object for a property's value, it first looks for the property's presence right within the object. If it is present, then its value will be returned. If it's not present there, then Javascript will persist and ask the object's constructor function's prototype object for the value of that property. Only if it's not present even there, javascript will admit failure.&lt;/p&gt;

&lt;p&gt;Actually that's not true. If that object also has a reference to yet another prototype object, then javascript fill follow the trail upwards till it gets the value or till it reaches a dead-end.&lt;/p&gt;

&lt;p&gt;With this idea in mind, we can now make the &lt;code&gt;john_car&lt;/code&gt; object inherit properties from the Car constructor by manipulating its prototype object reference. By default, the &lt;code&gt;john_car&lt;/code&gt; object will have a reference to its constructor's prototype through its &lt;code&gt;__proto__&lt;/code&gt; property. Only because of that, the 'instanceof' check above passed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far, our I20Asta function's prototype has nothing but a constructor property and the 'steering_type' property we added to it. It's of no use to us now considering that we need inheritance. To be able to inherit, what if we scrub I20Asta's current prototype object and make it point to another object? In particular, the object we want to inherit from - the Car? Let's do that right away.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// the key line that enables inheritance&lt;/span&gt;
&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The magic is done. But wait, since we scrubbed the old prototype object, we've lost the steering_type method we added to it. We need to add it again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;steering_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;power&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;  Now our john_car object has access to all of these: it's own properties&lt;/li&gt;
&lt;li&gt;  properties added in its constructor's prototype object&lt;/li&gt;
&lt;li&gt;  properties defined in it's supertype's prototype object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can now test the 3 inheritance ideas with success.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Redefine the objects&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 1. subtype should be instance of supertype&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//  true&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//  true&lt;/span&gt;

&lt;span class="c1"&gt;// 2. subtype should inherit properties from supertype&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;//  ['petrol', 'diesel', 'lpg']&lt;/span&gt;

&lt;span class="c1"&gt;// 3. subtype should be able to override properties defined in supertype&lt;/span&gt;
&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;electric&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;a_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;//  ['petrol', 'diesel', 'lpg']&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;electric&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This method of implementing inheritance is called &lt;strong&gt;"Prototype Chaining"&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disadvantage of Prototype Chaining
&lt;/h3&gt;

&lt;p&gt;With inheritance by prototype chaining, you can't have individual reference type properties inherited from the supertype. It will be shared across all objects. (Javascript reference types are objects, arrays, and user-defined custom objects, as opposed to primitive values. Variables referring to these items don't hold individual memory, instead they just act as pointers to the actual location of the reference types.) &lt;/p&gt;

&lt;p&gt;Notice that in the Car function, we have a wheels property which is an array. An array in javascript is a reference type. With this inheritance setup, try to ask both &lt;code&gt;john_car&lt;/code&gt; and &lt;code&gt;joe_car&lt;/code&gt; (another instance of I20Asta) for this property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;joe_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ['front', 'back']&lt;/span&gt;
&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ['front', 'back']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All seem fine. Or so it appears. Let's say john has added another wheel to his car's side. To reflect this, we add another item to his wheels property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;side&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back", "side"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now ask joe_car for its wheels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back", "side"]&lt;/span&gt;

&lt;span class="c1"&gt;// (2019 update: I cringe at the examples. Please forgive me.)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Inadvertently, we have updated Joe's wheels too! This is wrong. Joe didn't ask for an enhancement. As said earlier, this affects only the reference type properties. But that's enough of a deterrent to start searching for other inheritance methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Combination Inheritance Pattern = Prototype Chaining + Constructor Stealing
&lt;/h3&gt;

&lt;p&gt;That's a mouthful. But this is the most popular inheritance pattern used in javascript. At its core, it uses prototype chaining, but steals the supertype's constructor within the subtype constructor to rectify the problem discussed above. To implement this in the above example, you'd do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;front&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;back&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;available_fuel_options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;steering_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;manual&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// NOTE: THIS IS THE CRUCIAL STEP. Calling the supertype's constructor enables access to its properties individually for the objects.&lt;/span&gt;
  &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;steering_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;power&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// the key line that enables inheritance&lt;/span&gt;
&lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;john_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;joe_car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;I20Asta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All of the 3 inheritance tests discussed above works here too. You can test it. Now ask for wheels and try to manipulate them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back"]&lt;/span&gt;
&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back"]&lt;/span&gt;

&lt;span class="c1"&gt;// add a wheeel to john's car in the side&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;side&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back", "side"]&lt;/span&gt;

&lt;span class="c1"&gt;// Joe's car's wheels remain unaffected by the above change! It works ma!&lt;/span&gt;
&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back"]&lt;/span&gt;

&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;top&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;//  for whatever reason!&lt;/span&gt;
&lt;span class="nx"&gt;joe_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back", "top"]&lt;/span&gt;
&lt;span class="nx"&gt;john_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wheels&lt;/span&gt; &lt;span class="c1"&gt;// ["front", "back", "side"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;  Victory! We can now see that using this pattern, we are able to achieve perfect inheritance in javascript. Now go play. The World is your Javascripty Oyster!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ruby</category>
      <category>oops</category>
    </item>
    <item>
      <title>A Primer about Javascript's Prototype</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Sat, 14 Sep 2019 13:30:03 +0000</pubDate>
      <link>https://dev.to/npras/a-primer-about-javascript-s-prototype-1g85</link>
      <guid>https://dev.to/npras/a-primer-about-javascript-s-prototype-1g85</guid>
      <description>&lt;p&gt;Look, I know you are all cool people using the latest ES6 and 7 features, classes and VR and AR and all that. You use typescript, react and write CSS and HTML in JS file. Me, I'm old (10yrs industry experience) and I do ruby for full-time. I try to keep up with the JS trend, but then since I don't get that much time to play with these, I always lag behind.&lt;/p&gt;

&lt;p&gt;But I do have to write Javascript occasionally and once had to learn React in a short span of time to ship 2 projects for a client. What helped me in those times are the good understanding of the basics. I wrote this guide about prototype 3 years ago. But I still refer to it myself if I have to do some serious JS and it has always helped me get started with the new topics much quicker. I hope it helps you too.&lt;/p&gt;

&lt;p&gt;Here it is...&lt;/p&gt;

&lt;p&gt;If you are just learning Javascript, understanding clearly the concept of a javascript prototype might be crucial, but at the same time impossible. All the popular google search results point you to well explained articles that come from expert mouths. As in the below links.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/"&gt;http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/"&gt;http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/"&gt;http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="http://javascriptissexy.com/javascript-objects-in-detail/"&gt;http://javascriptissexy.com/javascript-objects-in-detail/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain"&gt;https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="http://blog.pluralsight.com/understanding-javascript-prototypes"&gt;http://blog.pluralsight.com/understanding-javascript-prototypes&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I find them lacking simple explanation. While you can take the following explanation as the "&lt;em&gt;Final Word of God About Prototype&lt;/em&gt;", I would treat this as a "&lt;em&gt;explain to me like I'm 5 (in programmer-age-speak)&lt;/em&gt;" guide and then proceed to checkout other info sources to cross-check and deepen the memory groove you would've formed.&lt;/p&gt;

&lt;p&gt;So, what's a prototype in javascript? When a function is created, it gets a &lt;strong&gt;special&lt;/strong&gt; property called 'prototype' which is just an Object. It's special because javascript specifically looks for a property named 'prototype' in a function or an object when it has to do certain operations. For the time being, I'd advise you to see 'prototype' as just a word, and not as a word with a meaning associated to it. Forget the association of the dictionary meaning of prototype to the word prototype. See it as just another property on a js object.  &lt;/p&gt;

&lt;h3&gt;
  
  
  A Function's Prototype
&lt;/h3&gt;

&lt;p&gt;So again, when a function is created, it gets a special property called 'prototype' which is just an Object.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'object'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can check all the examples in this post by opening Chrome's javascript console or by typing "node" if you have installed NodeJS installed. Javascript objects have their own properties.&lt;/p&gt;

&lt;p&gt;So if a function's prototype is an object, then it too must have some properties right? Let's find out.&lt;/p&gt;

&lt;p&gt;Using the handy, in-built &lt;code&gt;Object.getOwnPropertyNames(object)&lt;/code&gt; function, we can get an array of all the own properties of an object. Let's find out the own properties of the prototype object of the &lt;code&gt;sayHi&lt;/code&gt; function.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ["constructor"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The prototype object of &lt;code&gt;sayHi&lt;/code&gt; function has a single property called as 'constructor'. Well, what is it? Let's find out.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;
&lt;span class="c1"&gt;// outputs:&lt;/span&gt;
&lt;span class="c1"&gt;// function sayHi(){&lt;/span&gt;
&lt;span class="c1"&gt;// return 'hi';&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;  Hmm. Is it the same thing as our &lt;code&gt;sayHi&lt;/code&gt; function? Let's find that too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;
&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yes indeed. A function's prototype object has, initially, a special property called as 'constructor' which simply points back to the original function. Circle of life.&lt;/p&gt;

&lt;h3&gt;
  
  
  The purpose of the prototype property (or Why it is named prototype)
&lt;/h3&gt;

&lt;p&gt;When you create an object from a function, and when you ask the object the value of any property, javascript first looks for the property right within the object itself. If it finds, the property's value is returned. If not, then instead of simply stopping, javascript will then ask the function's prototype for the property's value. Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;, and my age is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;// 'John'&lt;/span&gt;
&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="c1"&gt;// 23&lt;/span&gt;
&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bio&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// I'm John, and my age is 23.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When we ask the john object for a 'name' property, it returns 'John' because it has a property named 'name' of its own. Similarly we can ask it's age and also ask it to speak its bio.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ["name", "age", "bio"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's add some additional properties, but not to the john object, or the Person function, but to the Person function's prototype object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sapiens&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;  We can even check that these 2 properties are now newly added only to the prototype object, and not to any of the objects created via Person (eg: john object).&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// outputs:&lt;/span&gt;
&lt;span class="c1"&gt;// ['constructor', 'species', 'sayName']&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// still the same as before&lt;/span&gt;
&lt;span class="c1"&gt;// ["name", "age", "bio"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now try asking the john object for a 'species' property, and also lets ask it to say its name, both of which are not own props of john.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt;
&lt;span class="c1"&gt;// 'Sapiens'&lt;/span&gt;

&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// "Hi, I'm John"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As you can see, the john object now has become simply awesome. When we ask it about a property that's not its own, it still looks up and asks its constructor's prototype object to see if it has these properties and returns their values if they exist.&lt;/p&gt;

&lt;p&gt;Did you also notice that the john object is able to do this even when the species and sayName props are added to prototype, &lt;em&gt;after&lt;/em&gt; john object's creation? That's too hot an awesomeness to handle. Now let's do one more thing.&lt;/p&gt;

&lt;p&gt;From the Person function, let's create another object named 'Joe'.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;joe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;// 'Joe'&lt;/span&gt;
&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="c1"&gt;// 35&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Let's ask Joe too his species and ask him to speak his name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt; &lt;span class="c1"&gt;// 'Sapiens'&lt;/span&gt;
&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// "Hi, I'm Joe"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From this we can infer that the properties assigned to the prototype object is accessible to all of the objects created from the function. But why? What difference does it make to have these properties defined right within the function itself?&lt;/p&gt;

&lt;p&gt;Functions and objects are real things occupying real space in the computer's memory. So when we define a property in the function, and an object is created, memory is allocated for those properties in the object. Even though john and joe both have same own properties - name, age, bio - because they are different objects, these properties too tend to take up separate space. On the other hand, the properties defined on the prototype - species and sayName - they are not duplicated. At any given instance in the application's runtime, these props occupy only one memory space. So when we ask both john and joe for these properties, javascript just gets the value from this shared location for both of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  An Object's Prototype
&lt;/h3&gt;

&lt;p&gt;So far we saw about a function's prototype property and its uses. Does an object have a prototype property too? It kinda has.&lt;/p&gt;

&lt;p&gt;An object's prototype simply points to the object's constructor's prototype. (To refresh, an object's constructor is the function that was used to create the object. john and joe's constructor are both the Person function.) But it's not accessible as the ecmascript implementation prohibits it. But still browsers and nodejs implementations expose an object's prototype via the &lt;strong&gt;proto&lt;/strong&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;
&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also check this in 2 more ways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;
&lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;A Prototype is neither a mystical Unicorn nor "version 0" of anything (the dictionary connotation).&lt;/p&gt;

&lt;p&gt;It's just a property on a javascript function, that initially has a 'constructor' property. The constructor property of the prototype object is just a pointer to the function itself. Properties defined in a prototype object are accessible to all the objects created via the function. An object doesn't have a prototype of its own. But its internal prototype property is simply a pointer to the object's constructor's prototype.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Rails Authentication From Scratch. Going Beyond Railscasts</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Fri, 13 Sep 2019 05:34:11 +0000</pubDate>
      <link>https://dev.to/npras/rails-authentication-from-scratch-going-beyond-railscasts-59i5</link>
      <guid>https://dev.to/npras/rails-authentication-from-scratch-going-beyond-railscasts-59i5</guid>
      <description>&lt;p&gt;If you have to implement authentication in your rails app, Devise is your safest option. Rolling your own is shooting yourself in the foot.&lt;/p&gt;

&lt;p&gt;But using Devise didn't feel like coding to me. It's like setting up your new mac by reading instructional blog posts around the net. Devise has great documentation and has all of your questions covered. You just follow them and you get industry level security.&lt;/p&gt;

&lt;p&gt;But it would be good coding practice if we can understand how Devise, and authentication in general works.&lt;/p&gt;

&lt;p&gt;So I implemented it from scratch following the famous &lt;a href="http://railscasts.com/episodes/250-authentication-from-scratch-revised"&gt;Ryan Bates tutorial&lt;/a&gt;. But the actual motivation came from Justin Searls, who in his recent talk &lt;a href="http://blog.testdouble.com/posts/2019-05-08-the-selfish-programmer"&gt;"Selfish Programmer"&lt;/a&gt; said he himself doesn't understand Devise and so implemented authentication from scratch for one of his side project. He implemented the usual workflows all by himself - the signup, sign in, forgot password, reset password etc - which helped him "keep all of his app's code within his head". (Which is a state you too should be in during the entire life of a project you are involved in.)&lt;/p&gt;

&lt;p&gt;I did the same thing. But after the main workflows, I started implementing other features similar to the way Devise had done them. I just cloned &lt;a href="https://github.com/plataformatec/devise"&gt;their repo&lt;/a&gt; and searched it for how a feature was implemented. For each of the feature that Devise supports, they take care of all possible edgecases. I could care less. So I took only the core of the feature and coded it.&lt;/p&gt;

&lt;p&gt;The features I implemented are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user registration&lt;/li&gt;
&lt;li&gt;authentication by email and password (signin and signout)&lt;/li&gt;
&lt;li&gt;remember and redirect to an auth-required page that the user tried to visit while logged-out, and then logged in&lt;/li&gt;
&lt;li&gt;user confirmation (only confirmed users can do certain/all actions)&lt;/li&gt;
&lt;li&gt;forgot password and password-reset&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the rest of the section, I'll explain how I implemented these. For missing pieces of the code here, you can find them in the actual repo: &lt;a href="https://github.com/npras/meaningless"&gt;https://github.com/npras/meaningless&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  The User database design
&lt;/h3&gt;

&lt;p&gt;Here's the users migration file showing all the fields, indices and the datatypes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CreateUsers&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;6.0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:password_digest&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:remember_token&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:password_reset_token&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;datetime&lt;/span&gt; &lt;span class="ss"&gt;:password_reset_sent_at&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:confirmation_token&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:unconfirmed_email&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;datetime&lt;/span&gt; &lt;span class="ss"&gt;:confirmation_sent_at&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;datetime&lt;/span&gt; &lt;span class="ss"&gt;:confirmed_at&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:remember_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;

    &lt;span class="c1"&gt;# I like to use empty lines to group related code&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password_reset_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;

    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:confirmation_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:unconfirmed_email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Code structure
&lt;/h3&gt;

&lt;p&gt;All of devise's controllers inherit from &lt;code&gt;DeviseController&lt;/code&gt;. I'd like my authentication functionalities to inherit from a &lt;code&gt;PrasDeviseController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(Not just for vanity reasons. Earlier I had user creation (signup) happen in UsersController, which made it hard to pull all common code in an ancenstral controller. That's when I realized Devise has a special thing called registrations which is where user creation happens. This allows us to make UsersController do non-authentication stuff while pulling out the user creation code to the authentication related code. &lt;em&gt;Ok, the naming is still vanity.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;PrasDevise&lt;/code&gt; controller hosts all the common methods that's used by the other auth-related code. As an example, this is a great place to put your recaptcha code if you are ever going to use them in any of your auth forms. I put them everywhere - signup, login, password-reset etc just to annoy the user (who's just me). Here are the 2 methods used for recaptcha: &lt;a href="https://github.com/npras/meaningless/blob/master/app/controllers/pras_devise/pras_devise_controller.rb#L70-L79"&gt;https://github.com/npras/meaningless/blob/master/app/controllers/pras_devise/pras_devise_controller.rb#L70-L79&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since the controllers are scoped, it'd be nice if the urls are scoped too. So here's how the &lt;code&gt;routes.rb&lt;/code&gt; file looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;module: :pras_devise&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:registrations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:create&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:confirmations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:sessions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:destroy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:password_resets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:update&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These controller files are all present in &lt;code&gt;app/controllers/pras_devise/&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  The User Registration workflow
&lt;/h3&gt;

&lt;p&gt;I'd like to allow all authenticated operations to be performed by confirmed users only. A confirmed user is one who had clicked a special link sent to their email that they claimed is theirs by signing up.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;create&lt;/code&gt; action, we lookup/initialize the user only by the &lt;code&gt;unconfirmed_email&lt;/code&gt; field and not by the &lt;code&gt;email&lt;/code&gt; field. Once the user confirms, we'll remove the email from unconfirmed field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;    &lt;span class="c1"&gt;# in pras_devise/registrations_controller.rb&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
      &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_or_initialize_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;unconfirmed_email: &lt;/span&gt;&lt;span class="n"&gt;user_params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
      &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_params&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_token_and_send_instructions!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;token_type: :confirmation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s2"&gt;"Check your email with subject 'Confirmation instructions'"&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_params&lt;/span&gt;
      &lt;span class="n"&gt;params&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password_confirmation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(Notice the 2nd line in the create action. I found it a nice way to assign a hash-like datastructure to all attributes of an activerecord object.)&lt;/p&gt;

&lt;h3&gt;
  
  
  The User Confirmation workflow
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;user.generate_token_and_send_instructions!&lt;/code&gt; method just generates a unique confirmation_token and sends an email to that user with a link containing that token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="c1"&gt;# in models/user.rb&lt;/span&gt;

  &lt;span class="c1"&gt;# token_type is:&lt;/span&gt;
  &lt;span class="c1"&gt;# confirmation for confirmation_token,&lt;/span&gt;
  &lt;span class="c1"&gt;# password_reset for password_reset_token&lt;/span&gt;
  &lt;span class="c1"&gt;# etc.&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_token_and_send_instructions!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token_type&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
    &lt;span class="n"&gt;generate_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;token_type&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="ss"&gt;_token"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;token_type&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="ss"&gt;_sent_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;utc&lt;/span&gt;
    &lt;span class="n"&gt;save!&lt;/span&gt;
    &lt;span class="no"&gt;UserMailer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;user: &lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:"email_&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;token_type&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="ss"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;deliver_later&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The mailer method looks like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="c1"&gt;# in mailers/user_mailer.rb&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;email_confirmation&lt;/span&gt;
    &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="vi"&gt;@email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unconfirmed_email&lt;/span&gt;
    &lt;span class="vi"&gt;@token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirmation_token&lt;/span&gt;

    &lt;span class="n"&gt;mail&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="vi"&gt;@email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;subject: &lt;/span&gt;&lt;span class="s2"&gt;"Confirmation instructions"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the email itself looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="vi"&gt;@email&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;You can confirm your account email through the link below:&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="s1"&gt;'Confirm my account'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confirmation_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;confirmation_token: &lt;/span&gt;&lt;span class="vi"&gt;@token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The link looks something like this: &lt;code&gt;http://example.com/confirmations/111?confirmation_token=sOmErandomToken123&lt;/code&gt; where &lt;code&gt;111&lt;/code&gt; is the user's id which is irrelevant here. We only need it because we are defining the related controller action in a restful manner.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;confirmations_ocntroller#show&lt;/code&gt; action we lookup the user by the &lt;code&gt;confirmation_token&lt;/code&gt; param. If the token isn't expired yet, then we confirm them by marking the &lt;code&gt;unconfirmed_email&lt;/code&gt; as nil and saving the record.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# confirmation_controller.rb&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
      &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;confirmation_token: &lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:confirmation_token&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirmation_token_expired?&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;new_registration_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;alert: &lt;/span&gt;&lt;span class="s2"&gt;"Confirmation token has expired. Signup again."&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unconfirmed_email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unconfirmed_email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirmed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;utc&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s2"&gt;"You are confirmed! You can now login."&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;alert: &lt;/span&gt;&lt;span class="s2"&gt;"No user found for this token"&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that if the token expired, we are redirecting to the signup page. If the user now tries to signup with the same email, it will still work because there, in &lt;code&gt;registrations_controller#create&lt;/code&gt; we use &lt;code&gt;User.find_or_initialize_by&lt;/code&gt; rather than &lt;code&gt;User.new&lt;/code&gt; every time someone attempts to signup.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Sign-In / Sign-Out workflow
&lt;/h3&gt;

&lt;p&gt;This is very straightforward.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find the user from db based on the email incoming from the sign-in form&lt;/li&gt;
&lt;li&gt;try to authenticate the user with the incoming password&lt;/li&gt;
&lt;li&gt;If the authentication succeeds, create a unique &lt;code&gt;remember_token&lt;/code&gt;, encrypt and save it in a cookie.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;login!&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;after_sign_in_path_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s2"&gt;"Logged in!"&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Email or password is invalid"&lt;/span&gt;
        &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;login!&lt;/span&gt;
      &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember_token&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:remember_token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:remember_me&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permanent&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:remember_token&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember_token&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:remember_token&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember_token&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the user checked the &lt;code&gt;Remember me&lt;/code&gt; checkbox in the sign in form, then we create a permanent cookie, otherwise it's just a normal one.&lt;/p&gt;

&lt;p&gt;If the authentication succeeds, we redirect the user to the page they previously attempted to visit unsuccessfully because they were un-authenticated at the time. This part of the code is taken straight from Devise. It's all in the parent controller &lt;code&gt;PrasDeviseController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how it's implemented in detail...&lt;/p&gt;

&lt;h3&gt;
  
  
  Redirect to specific page after sign in
&lt;/h3&gt;

&lt;p&gt;To do this, first you need to save each page the user visits in a session (a fancy cookie). But not all page should be saved. Devise saves only requests that satisfy all of these conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the request should be a GET request. Anything else sounds dangerous and is not simple to implement either. (Here's an interesting &lt;a href="https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect"&gt;stack overflow discussion&lt;/a&gt; about this.)&lt;/li&gt;
&lt;li&gt;the request should not be an ajax request&lt;/li&gt;
&lt;li&gt;the request should be for an action that doesn't come from any PrasDevise controller. ie, it shouldn't be for pages like sign-in, sign-up, forgot-password forms etc. Doesn't make sense&lt;/li&gt;
&lt;li&gt;the request format should be of html only&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These kind of requests are then stored in a session cookie with the key &lt;code&gt;:user_return_to&lt;/code&gt;. Once the user successfull logs in, then the &lt;code&gt;sessions_controller#create&lt;/code&gt; action redirects them to the correct&lt;/p&gt;

&lt;p&gt;So, here's a before_action callback that's called on every request to the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# in pras_devise_controller.rb&lt;/span&gt;

    &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="ss"&gt;:store_user_location!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;if: :storable_location?&lt;/span&gt;


    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;storable_location?&lt;/span&gt;
      &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="n"&gt;is_navigational_format?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_a?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PrasDevise&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PrasDeviseController&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xhr?&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_navigational_format?&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"*/*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:html&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request_format&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;request_format&lt;/span&gt;
      &lt;span class="vi"&gt;@request_format&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;try&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;store_user_location!&lt;/span&gt;
      &lt;span class="c1"&gt;# :user is the scope we are authenticating&lt;/span&gt;
      &lt;span class="c1"&gt;#store_location_for(:user, request.fullpath)&lt;/span&gt;
      &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extract_path_from_location&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullpath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_return_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;URI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;URI&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;InvalidURIError&lt;/span&gt;
      &lt;span class="kp"&gt;nil&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_path_from_location&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parse_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; 
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remove_domain_from_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_fragment_back_to_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;remove_domain_from_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/\A\/+/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;compact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'?'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_fragment_back_to_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;compact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'#'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;after_sign_in_path_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource_or_scope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;is_navigational_format?&lt;/span&gt;
        &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user_return_to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_return_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(It all came from Devise.)&lt;/p&gt;

&lt;h3&gt;
  
  
  The Password Reset workflow
&lt;/h3&gt;

&lt;p&gt;For password reset, you need 4 actions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; shows the form where the user would input their email. &lt;/li&gt;
&lt;li&gt;It would then be submitted to &lt;code&gt;create&lt;/code&gt; where the app would generate a &lt;code&gt;password_reset_token&lt;/code&gt; and email it to the incoming email.&lt;/li&gt;
&lt;li&gt;The user would then click the link in the email which would take him to a &lt;code&gt;password edit&lt;/code&gt; page where the user is found by the &lt;code&gt;password_reset_token&lt;/code&gt; from the link.&lt;/li&gt;
&lt;li&gt;Once the user fills the form with the new password and submits, it will go to the &lt;code&gt;update&lt;/code&gt; action which saves the new password in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the controller code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# password_resets_controller.rb&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
      &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
      &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_token_and_send_instructions!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;token_type: :password_reset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s2"&gt;"If you had registered, you'd receive password reset email shortly"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;edit&lt;/span&gt;
      &lt;span class="n"&gt;set_user&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;alert: &lt;/span&gt;&lt;span class="s2"&gt;"Cannot find user!"&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;
      &lt;span class="n"&gt;set_user&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;utc&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;password_reset_sent_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hours&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;new_password_reset_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;alert: &lt;/span&gt;&lt;span class="s2"&gt;"Password reset has expired!"&lt;/span&gt;
      &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password_update_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s2"&gt;"Password has been reset!"&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_user&lt;/span&gt;
      &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;password_reset_token: &lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;password_update_params&lt;/span&gt;
      &lt;span class="n"&gt;params&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password_confirmation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that when the update form is submitted, we make sure the password_reset email was sent very recently. We don't want users to abuse this functionality.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;email_password_reset.html.erb&lt;/code&gt; template looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;To reset your password, click the URL below.

&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="n"&gt;edit_password_reset_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;password_reset_token&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;edit_password_reset_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;password_reset_token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;

If you did not request your password to be reset, just ignore this email and your password will continue to stay the same.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;It's great to spend time looking under the hood of any library. With the help of example codes and test cases in the Devise repo, I was able to put pieces together and find out how some of the main functionalities work. I also came across many samples of succint and beautiful code, especially their test cases. Just by using minitest and mocha they've written short but easily readable test cases.&lt;/p&gt;

&lt;p&gt;Nothing is mysterious if you  take the time to explore it with curiosity.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>security</category>
    </item>
    <item>
      <title>Tips to save time with alias command (not what you think)</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Tue, 10 Sep 2019 05:20:19 +0000</pubDate>
      <link>https://dev.to/npras/save-time-with-alias-command-not-what-you-think-197n</link>
      <guid>https://dev.to/npras/save-time-with-alias-command-not-what-you-think-197n</guid>
      <description>&lt;p&gt;Tuesdays are not as cool as Mondays or Fridays. But it's never shabby for productivity tidbits. Here's two for today. The &lt;code&gt;alias&lt;/code&gt; shell command has saved so many fingers over the years. I figured 2 tips that could save even more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 1: Echo the command first
&lt;/h2&gt;

&lt;p&gt;I have &lt;code&gt;murder&lt;/code&gt; as an alias for &lt;code&gt;kill -9&lt;/code&gt;. So when I have to kill any process, I find it's id (let's say it's &lt;code&gt;2345&lt;/code&gt;) and then just run &lt;code&gt;murder 2345&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's cool. I've been using it for years now. But over time, I might forget the actual command itself. It's important though. If you are fiddling with a server or a docker container you won't have access to your aliases and your time-saving habit will cost time there. Instead I define my alias like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;murder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"echo 'cmd: kill -9 ' &amp;amp;&amp;amp; kill -9 "&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, when I run the &lt;code&gt;murder&lt;/code&gt; command, it first echoes the actual command and then does the killing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;murder 62210
cmd: &lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'll spend at least half a second to read the output which will make me glance at the actual command. It might come in handy some day!&lt;/p&gt;

&lt;p&gt;Here are more examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Shell command to run a simple http server to serve current directory's&lt;/span&gt;
&lt;span class="c"&gt;# files.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;serve
cmd: ruby &lt;span class="nt"&gt;-run&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; httpd &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 3000
&lt;span class="o"&gt;[&lt;/span&gt;2019-09-10 10:15:33] INFO  WEBrick 1.4.2
&lt;span class="o"&gt;[&lt;/span&gt;2019-09-10 10:15:33] INFO  ruby 2.6.3 &lt;span class="o"&gt;(&lt;/span&gt;2019-04-16&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;x86_64-darwin16]
&lt;span class="o"&gt;[&lt;/span&gt;2019-09-10 10:15:33] INFO  WEBrick::HTTPServer#start: &lt;span class="nv"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;62753 &lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, the first output line is the actual command. The alias is setup as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;serve&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"echo 'cmd: ruby -run -e httpd . -p 3000' &amp;amp;&amp;amp; ruby -run -e httpd . -p 3000"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All you need is the &lt;code&gt;echo&lt;/code&gt; command and the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator to define it like so.&lt;/p&gt;

&lt;p&gt;You can even put this in a shell function. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;psgrep&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'cmd: ps -ef | grep -i '&lt;/span&gt;
  ps &lt;span class="nt"&gt;-ef&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;psgrep&lt;/code&gt; would show the matching line-items from the &lt;code&gt;ps&lt;/code&gt; command's output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;psgrep irb
cmd: ps &lt;span class="nt"&gt;-ef&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;
  501 62214 60386   0 10:11AM ttys003    0:00.00 &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; irb
  501 62210 61813   0 10:11AM ttys004    0:00.36 irb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 2: Create alias for aliases
&lt;/h2&gt;

&lt;p&gt;Certain commands can be grouped. For example there are many disk-space related commands. &lt;code&gt;du -sh&lt;/code&gt; lists the size of the current directory. &lt;code&gt;df -h&lt;/code&gt; lists the disk-space of the whole system. &lt;code&gt;du -sh * | sort -k1,1rn | head&lt;/code&gt; sorts the current directories contents by size. We can create aliases for these so that we don't have to remember the complex flags and pipes. But what if we forget the aliases themselves?&lt;/p&gt;

&lt;p&gt;You can simply type &lt;code&gt;alias&lt;/code&gt; which will print out all the aliases you've defined, and then maybe you can grep just the items you want. But it's tedious and you still have to remember certain parts of what you are looking for.&lt;/p&gt;

&lt;p&gt;There's a better way. Create an alias that just echoes these aliases with a short description of what they do. You can group them by functionality. For example, in the case of the commands above, they are all about calculating the disk-size. So I defined an easy-to-remember alias called &lt;code&gt;size&lt;/code&gt;. It's output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;size
dush - current &lt;span class="nb"&gt;dir &lt;/span&gt;size
dirszsort - &lt;span class="nb"&gt;sort dir &lt;/span&gt;contents by size
disksz - disk size
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now I'll know which of these I need at the moment and just run it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;disksz
cmd: &lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt;
Filesystem      Size  Used Avail Use% Mounted on
/dev/disk1      465G  368G   97G  80% /
/dev/disk2      2.0M  140K  1.9M   7% /private/var/folders/jn/bnyd1y_503xgka61g50ygf740000te/T/NVProtectedEditingSpace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;size&lt;/code&gt; alias is defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"echo 'dush - current dir size' &amp;amp;&amp;amp;
            echo 'dirszsort - sort dir contents by size' &amp;amp;&amp;amp;
            echo 'disksz - disk size'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, could someone please create a repo with these nice group aliases so all can contribute and/or steal?&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>bash</category>
    </item>
    <item>
      <title>Everything is a search problem. Master your search skills.</title>
      <dc:creator>Prasanna Natarajan</dc:creator>
      <pubDate>Sun, 08 Sep 2019 15:58:27 +0000</pubDate>
      <link>https://dev.to/npras/everything-is-a-search-problem-master-your-search-skills-4iaf</link>
      <guid>https://dev.to/npras/everything-is-a-search-problem-master-your-search-skills-4iaf</guid>
      <description>&lt;p&gt;Once during a meeting, my manager said to us &lt;em&gt;"In this age of information and the internet, everything is a search problem. If you know how to search, half your troubles are over."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As a programmer most of your days are spent editing code written by either you, or other people. These coding tasks have objectives. You need information to fulfill them. Information about your coding language, about the project your are working on, and about how it fits in the big picture of your company's success.&lt;/p&gt;

&lt;p&gt;The places from where you could get these information can be classified into 4 categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The person who assigned you this task&lt;/strong&gt;. Your project lead or manager. If you get to pick the task instead of getting assigned, then the product owner might be this person. (If you are the product owner, then it's the business stakeholder who wants this task to be done. &lt;em&gt;We aren't talking beyond this level&lt;/em&gt;.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The system&lt;/strong&gt;. It's your company's knowledge repository. Confluence, wiki softwares etc. And other secondary places like google drive, dropbox, trello, github (source code is info too), slack, your email (recurring news coming from a distribution-list you are part of) etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other people&lt;/strong&gt;. More often than not, you'll have to pull information from others, within your team and across teams, people in partner services, clients etc. Ping or email them or setup a meeting to get the information out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You&lt;/strong&gt;. Well that's what you get paid for right? Your unique set of technical skills and the knowledge you can draw from your past experiences contribute the most to solve the puzzle at hand.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the puzzle at hand, assuming you've got everything you need to know about it from your manager and other people, what's left is the system and you. &lt;strong&gt;It pays to be able to find information quickly from the system using your advanced search skills&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's a list of places where you could practice and improve your search skills.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Search for a word across a project:

&lt;ul&gt;
&lt;li&gt;search for a whole word, case-sensitive or case-insensitive&lt;/li&gt;
&lt;li&gt;search using regular expression&lt;/li&gt;
&lt;li&gt;search within a specific sub-folder&lt;/li&gt;
&lt;li&gt;search within a file, forward or backward&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Search for a file:

&lt;ul&gt;
&lt;li&gt;within a project and across the system&lt;/li&gt;
&lt;li&gt;within current directory and within all levels of subdirectories of current directory&lt;/li&gt;
&lt;li&gt;fuzzy search (where you type part of the absolute path as well)&lt;/li&gt;
&lt;li&gt;ignoring certain files and directories (modern search tool like &lt;a href="https://github.com/BurntSushi/ripgrep"&gt;ripgrep&lt;/a&gt; can respect your &lt;code&gt;.gitignore&lt;/code&gt; files)&lt;/li&gt;
&lt;li&gt;catch-all item: google &lt;a href="https://www.google.com/search?q=vim+advanced+search"&gt;"vim advanced search"&lt;/a&gt; or &lt;a href="https://www.google.com/search?q=vs+code+advanced+search"&gt;"vs code advanced search"&lt;/a&gt;. Read the manual and blog posts you find. Print them out, keep by your desk and deliberately practice.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Learn to do these two items above both within your favorite editor and from terminal command prompt (&lt;code&gt;grep&lt;/code&gt; and &lt;code&gt;find&lt;/code&gt; family of commands).&lt;/li&gt;
&lt;li&gt;Search for a bug (aka debug). Ok, it's not fair to put debugging as yet another line-item. But it's still a search and you need to master your stack's best debugging tools so you can find and squash that bug.&lt;/li&gt;
&lt;li&gt;Your code has a lot of dependencies ('gems' in ruby, 'jars' in java, 'packages' in node etc). Your debugging rabbit hole might lead you to code within these dark zones. You need to be able to quickly search them (eg: in ruby we can run the terminal command &lt;code&gt;bundle open activerecord&lt;/code&gt; which will open the locally installed library's root path in an editor)&lt;/li&gt;
&lt;li&gt;Learn to navigate around your code. I use &lt;a href="https://github.com/universal-ctags/ctags"&gt;ctags&lt;/a&gt; in vim to go to method/class/module definitions. But VS code and sublime text have much better version of these functionalities. (I was blown away when &lt;a href="https://marketplace.visualstudio.com/items?itemName=pranaygp.vscode-css-peek"&gt;VS code showed the css rule&lt;/a&gt; defined for the class my cursor was under in a pop-up!)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Searching Jira and Confluence
&lt;/h3&gt;

&lt;p&gt;Jira is the most widely used issue-tracking system. Tell yourself to love it. It's &lt;a href="https://confluence.atlassian.com/jiracore/blog/2015/07/search-jira-like-a-boss-with-jql"&gt;JQL (Jira Query Language)&lt;/a&gt; is such a joy to use (because it auto-suggests unlike psql or mysql). You can create advanced queries and save them so you don't have to type the query again.&lt;/p&gt;

&lt;p&gt;Use it quickly to find all sorts of tickets. I have saved queries for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;finding tickets my teammates are assigned to, or even just participate etc.&lt;/li&gt;
&lt;li&gt;specific kinds of tickets. Eg: I often have to run a specific kind of task in production. The details are in the previous such ticket. I have a query that returns all instances of such deployment tickets.&lt;/li&gt;
&lt;li&gt;mentions of my project in any jira ticket in the whole company.&lt;/li&gt;
&lt;li&gt;mentions of a word within 2 weeks. Because in this case only the recency matters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have created and saved all the queries you can think of, go and ask your lead or other experienced person about their own saved queries. You will get a lot of ideas of how to search jira and get what you want.&lt;/p&gt;

&lt;p&gt;And do I have to say there's also the equivalent &lt;a href="https://developer.atlassian.com/server/confluence/advanced-searching-using-cql/"&gt;CQL&lt;/a&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching Email
&lt;/h3&gt;

&lt;p&gt;Most people from "&lt;em&gt;English-is-not-my-native-tongue&lt;/em&gt;" countries don't know to use basic email features. Once you read a new mail, you process it by either deleting it or archiving it or by applying some labels and moving it off of your site.&lt;/p&gt;

&lt;p&gt;But you can find many junior developers, especially from developing countries, keep all their mails sitting in the inbox. For years. (Just look at the "Inbox (9876)" counter). They don't know the concept of inbox or archiving. (I blame it on the words used to describe them. Instead it should be "&lt;em&gt;tabletop&lt;/em&gt;" (or "&lt;em&gt;desktop&lt;/em&gt;") and "&lt;em&gt;basement rack-room&lt;/em&gt;".)&lt;/p&gt;

&lt;p&gt;Anyway, learn the &lt;a href="https://support.google.com/a/users/answer/9297685?hl=en"&gt;email basics&lt;/a&gt; first (assuming you use gmail). Because, to search it effectively, you'll first need to put things there effectively in a structured manner. Use "&lt;em&gt;filter messages like this&lt;/em&gt;" liberally and apply labels of all sorts (emails from your immediate boss, emails from your teammates, automated emails from the services you use like jenkins, jira, code review tool etc.)&lt;/p&gt;

&lt;p&gt;And then, learn to &lt;a href="https://support.google.com/mail/answer/7190"&gt;search it&lt;/a&gt; effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching Google
&lt;/h3&gt;

&lt;p&gt;Your coding problems aren't older than 2008 which is when StackOverflow came into existence. There's a green-ticked solution waiting just for you. In the worst case, there's somebody who've toiled hard to write a blog post detailing the solution for your problem.&lt;/p&gt;

&lt;p&gt;You just need to ask google nicely to show that page to you. You need to know &lt;a href="https://ahrefs.com/blog/google-advanced-search-operators/"&gt;Google-Fu&lt;/a&gt;. Learn to search for specic words, ignore words, search within a set of websites, search for specific filetypes etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching Other Tools
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Slack - &lt;a href="https://get.slack.help/hc/en-us/articles/202528808-Search-in-Slack"&gt;here&lt;/a&gt; and &lt;a href="https://slackhq.com/next-level-searching-in-slack"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Github - &lt;a href="https://help.github.com/en/articles/about-searching-on-github"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Google Drive - &lt;a href="https://support.google.com/drive/answer/2375114?co=GENIE.Platform%3DDesktop&amp;amp;hl=en"&gt;here&lt;/a&gt;. I didn't know that drive can also search the contents of the files.&lt;/li&gt;
&lt;li&gt;Your internal log search/parse tools. Eg: &lt;a href="https://www.elastic.co/guide/en/kibana/current/search.html"&gt;Kibana&lt;/a&gt; (they have KQL and LQL. And you only know SQL.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;My manager went on to say that if your company can't find a good candidate then it's still a search problem, not a &lt;em&gt;lack-of-candidate&lt;/em&gt; problem. It then led to discussions of how to search certain websites to find the best candidates.&lt;/p&gt;

&lt;p&gt;In the night I dreamed of him telling one of our unmarried colleague "that's a search problem too. You just need to know the advanced filters."&lt;/p&gt;

&lt;p&gt;So... learn to ask interesting questions and also where and whom to ask them. You don't deserve to be frustrated.&lt;/p&gt;

&lt;p&gt;In our jobs, not every day we get to write great code showing off our algorithm, data-structure and recursion skills. It's mostly CRUD. But we could solve important business problems by deliberately seeking the right information and putting two and two together. I look up to people who can do that and try to up my own search game.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please share in comments any other 'searches' that might help us all become better at our jobs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(Thanks to my manager for bringing this to my attention.)&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>editor</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
