<?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: Edison Yap</title>
    <description>The latest articles on DEV Community by Edison Yap (@edisonywh).</description>
    <link>https://dev.to/edisonywh</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%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg</url>
      <title>DEV Community: Edison Yap</title>
      <link>https://dev.to/edisonywh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edisonywh"/>
    <language>en</language>
    <item>
      <title>Real World Example with Elixir Protocol</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Sat, 13 Mar 2021 22:02:30 +0000</pubDate>
      <link>https://dev.to/edisonywh/real-world-example-with-elixir-protocol-57ec</link>
      <guid>https://dev.to/edisonywh/real-world-example-with-elixir-protocol-57ec</guid>
      <description>&lt;p&gt;Today I'm going to write about Protocols in Elixir, and how I'm using them in my production app, &lt;a href="https://slickinbox.com"&gt;Slick Inbox&lt;/a&gt;, a newsletter inbox app.&lt;/p&gt;

&lt;p&gt;Protocol had always been a bit of a mystery to me, I've always been confused between it and behaviour, like when should I use one over another? I tried looking up resources on the web but I didn't manage to find much beyond the basics. I was interested in real world application, and learning how it could be used to apply to my situation. That's why I'm writing my journey here, perhaps someone could find this useful!&lt;/p&gt;




&lt;p&gt;I was refactoring &lt;a href="https://slickinbox.com"&gt;Slick Inbox&lt;/a&gt;, and one of the things I wanted to do is to identify my domains, group them up and enforce proper boundary/domain design. There's an excellent package, &lt;a href="https://github.com/sasa1977/boundary"&gt;boundary&lt;/a&gt; in Elixir that can help with that, but the first step is for me to identify my domains first.&lt;/p&gt;

&lt;p&gt;Phoenix ships with the concept of Contexts, which sort of nudges you in the right direction, but in the beginning things are pretty unclear so it is difficult to map out the domain upfront. Instead, I think it makes more sense to run your app in production and let the domain emerges itself.&lt;/p&gt;

&lt;p&gt;Anyway, while I was doing that, I identified a few domains, and one of it is what I now call &lt;code&gt;Messaging&lt;/code&gt;. The responsibility of this domain is to communicate with users, and in Slick's current set-up, there are three ways to do that: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Push Notification (when user receives a newsletter)&lt;/li&gt;
&lt;li&gt;Email (when I send user an email to their personal email address)&lt;/li&gt;
&lt;li&gt;Issue (when I send user an Issue in their Slick inbox)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two are self-explanatory. For the third one, Issue is a Slick entity. Slick is a newsletter inbox app, when an author sends users an email, it gets parsed into an Issue, which is then shown on the users' Inbox. One could argue that it's fundamentally the same thing as email, and one won't be too far off, but sending an Issue is a closed-loop action, there's no need to make a call to an Email Service Provider.&lt;/p&gt;

&lt;p&gt;Anyway, now that I've established the three data type I want to be able to &lt;code&gt;send&lt;/code&gt;/&lt;code&gt;deliver&lt;/code&gt; to user, I thought this was a perfect use case for protocols. From what I know, protocols are meant for doing an action based on data type, which is exactly what I have right now. Protocols it is then!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I start off by describing what action I want&lt;/strong&gt;. If you paid attention to my language earlier, you can see that I want to &lt;code&gt;send/deliver&lt;/code&gt; something. That's my action. With this in mind, I created a protocol called &lt;code&gt;Courier&lt;/code&gt;, which does one thing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defprotocol&lt;/span&gt; &lt;span class="no"&gt;Courier&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;deliver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;Now, the next thing is to implement the protocol.&lt;/p&gt;

&lt;p&gt;For Push Notifications, I use the &lt;a href="https://github.com/codedge-llc/pigeon/"&gt;Pigeon&lt;/a&gt; library to interface with FCM. My initial implementation was pretty crude, I was passing data around, and the function signature looked something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;notify&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="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="o"&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;It looked fine to me at first, I am passing in the &lt;code&gt;user&lt;/code&gt;, then the &lt;code&gt;title&lt;/code&gt; and then the &lt;code&gt;message&lt;/code&gt;, but in reality there's a lot mangling with the parameters to fit into what Pigeon/FCM expect, and I figured with the protocol idea, why don't I create a new struct to hold this data? That's where I came up with &lt;code&gt;Push&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Push&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;defstruct&lt;/span&gt; &lt;span class="ss"&gt;user:&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="ss"&gt;body:&lt;/span&gt; &lt;span class="p"&gt;%{},&lt;/span&gt; &lt;span class="ss"&gt;data:&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;I would also need to implement the protocol, so it ended up looking something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Push&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;defimpl&lt;/span&gt; &lt;span class="no"&gt;Courier&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;deliver&lt;/span&gt;&lt;span class="p"&gt;(%{&lt;/span&gt;&lt;span class="ss"&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="ss"&gt;body:&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;data:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fetch_tokens&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&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;do_send_push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&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="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, I implemented my Context APIs like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Messaging&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;send_push&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="n"&gt;issue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&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="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="no"&gt;Push&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&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="ss"&gt;body:&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;data:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Courier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deliver&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I only send push notifications when an issue arrives in their inbox, so I can just pass in the Issue directly here. Keep in mind though, my Protocol still knows nothing about Issue, it has the perfect shape of data it needs with &lt;code&gt;%Push{}&lt;/code&gt; to do its FCM magic.&lt;/p&gt;

&lt;p&gt;I am pretty happy with this so far, and that's basically it really. The other two context APIs look pretty much the same, so there's not much to talk about:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Messaging&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;send_mail&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="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;assigns&lt;/span&gt; &lt;span class="p"&gt;\\&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="no"&gt;Mail&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&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="ss"&gt;template:&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;assigns:&lt;/span&gt; &lt;span class="n"&gt;assigns&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Courier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deliver&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;def&lt;/span&gt; &lt;span class="n"&gt;send_issue&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="n"&gt;issue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Courier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deliver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;issue&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Frankly, I still don't really have the heuristic to decide when to use protocols vs behaviour, but I hoped by sharing my experience it could maybe bring clarity to you.&lt;/p&gt;




&lt;p&gt;Do you have any heuristics that you can share when to use protocols vs behaviours, or do you have resources you'd recommend about domain driven design (I'm especially interested in Phoenix code), I'd love if you could share them with me!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>erlang</category>
    </item>
    <item>
      <title>ElixirConf US 2019 and the future of Elixir</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Fri, 30 Aug 2019 15:44:32 +0000</pubDate>
      <link>https://dev.to/edisonywh/elixirconf-us-2019-and-the-future-of-elixir-13n5</link>
      <guid>https://dev.to/edisonywh/elixirconf-us-2019-and-the-future-of-elixir-13n5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;For those of you who aren't familiar with Elixir but are interested in the language, I compiled a little &lt;code&gt;Resources&lt;/code&gt; section down below, feel free to get some inspiration from there! &lt;strong&gt;What's a better time to jump into Elixir?&lt;/strong&gt; :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As some of you may know, ElixirConf US 2019 is currently running over at Gaylord Rockies Resort, Colorado!&lt;/p&gt;

&lt;p&gt;The best thing about it is, even though the conference is still running, the videos are already out, &lt;a href="https://www.youtube.com/playlist?list=PLqj39LCvnOWYTNs1n3ZNMSNO3Svv_XweT"&gt;check them out over at YouTube&lt;/a&gt;! I think it's pretty amazing that the videos come out so quicky so people who aren't able to attend don't feel as left out :)&lt;/p&gt;

&lt;p&gt;There are a plethora of interesting things that are happening and frankly I haven't even had time to go through all of them yet! Some of those are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/lumen/lumen"&gt;Lumen&lt;/a&gt;, &lt;strong&gt;an alternative BEAM implementation implemented in Rust, designed for WebAssembly&lt;/strong&gt;, although the main goal is not to replace BEAM, it does seem like it could eventually head there! (Side note, I think a Rust-based VM is pretty exciting, just like what &lt;a href="https://github.com/archseer/enigma"&gt;Enigma&lt;/a&gt; is trying to do.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=oUZC1s1N42Q&amp;amp;list=PLqj39LCvnOWYTNs1n3ZNMSNO3Svv_XweT&amp;amp;index=8&amp;amp;t=0s"&gt;Elixir's Future&lt;/a&gt; (Stability + Extensibility, better deployment experience, better tooling experience like tools to help aid Code Analysis, ExUnit now diffs for pattern matching as well)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just thought we could start a conversation here on dev.to, otherwise, the Elixir community is also on elixirforum.com and also very active on the Slack channel over at &lt;a href="https://elixir-slackin.herokuapp.com"&gt;https://elixir-slackin.herokuapp.com&lt;/a&gt;, join us!&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;If you're new to Elixir, here are some great resources to get you started:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://elixir-lang.org"&gt;The official Elixir website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://elixirschool.com/en/"&gt;ElixirSchool&lt;/a&gt;, a community-driven website to help make Elixir easily accessible!&lt;/li&gt;
&lt;li&gt;The amazing books, &lt;a href="https://pragprog.com/book/elixir16/programming-elixir-1-6"&gt;Programming Elixir by Dave Thomas&lt;/a&gt; and also &lt;a href="https://www.manning.com/books/elixir-in-action-second-edition"&gt;Elixir in Action by Sas̄a Jurić&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;My personal favourite, &lt;a href="https://pragmaticstudio.com/elixir"&gt;Elixir and the OTP&lt;/a&gt;, video-based tutorial from Pragmatic Studio. I basically learned my Elixir crafts from here!&lt;/li&gt;
&lt;li&gt;Other video resources like &lt;a href="https://elixircasts.io"&gt;ElixirCasts&lt;/a&gt; and &lt;a href="https://alchemist.camp"&gt;Alchemist Camp&lt;/a&gt; are also great!&lt;/li&gt;
&lt;li&gt;If you're into podcast, I'm a big fan of &lt;a href="http://elixirtalk.com/"&gt;Elixir Talk&lt;/a&gt;, they're perfect for your commute so definitely check them out!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P.S: With courtesy from &lt;a href="https://elixirforum.com/search?q=coupon"&gt;ElixirForum&lt;/a&gt; (probably the biggest Elixir community forum?), you can sometimes get up to 40% off the learning resources! Just try to input &lt;code&gt;elixirforum&lt;/code&gt; whenever you try to checkout :)&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>techtalks</category>
    </item>
    <item>
      <title>Introducing `Rocketman`, a gem to help you do Pub/Sub code in Pure Ruby. No Rails needed!</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Sat, 10 Aug 2019 15:52:55 +0000</pubDate>
      <link>https://dev.to/edisonywh/introducing-rocketman-a-gem-to-help-you-do-pub-sub-code-in-pure-ruby-no-rails-needed-2pgf</link>
      <guid>https://dev.to/edisonywh/introducing-rocketman-a-gem-to-help-you-do-pub-sub-code-in-pure-ruby-no-rails-needed-2pgf</guid>
      <description>&lt;p&gt;Publishing my second Ruby gem here with the community, introducing &lt;code&gt;Rocketman&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;Rocketman is a gem that introduces Pub-Sub mechanism within Ruby code.&lt;/p&gt;

&lt;p&gt;As with all Pub-Sub mechanism, this greatly decouples your upstream producer and downstream consumer, allowing for scalability, and easier refactor when you decide to move Pub-Sub to a separate service.&lt;/p&gt;

&lt;p&gt;Rocketman also works without Rails.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/edisonywh" rel="noopener noreferrer"&gt;
        edisonywh
      &lt;/a&gt; / &lt;a href="https://github.com/edisonywh/rocketman" rel="noopener noreferrer"&gt;
        rocketman
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      🚀 Rocketman help build event-based/pub-sub code in Ruby
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Rocketman&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/edisonywh/rocketman./rocketman.jpg"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fedisonywh%2Frocketman.%2Frocketman.jpg" alt="rocketman"&gt;&lt;/a&gt;
&lt;em&gt;yes, I know it says Starman on the image&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;🎶 And I think it's gonna be a long long time 'Till touch down brings me round again to find 🎶&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Rocketman is a gem that introduces Pub-Sub mechanism within your Ruby code.&lt;/p&gt;
&lt;p&gt;The main goal of Rocketman is not to replace proper message buses like Redis PubSub/Kafka, but rather be a stepping stone. You can read more about the &lt;a href="https://github.com/edisonywh/rocketman#why-use-rocketman-rather-than-a-proper-message-bus-eg-redis-pubsubkafka" rel="noopener noreferrer"&gt;rationale behind the project&lt;/a&gt; down below.&lt;/p&gt;
&lt;p&gt;As with all Pub-Sub mechanism, this greatly decouples your upstream producer and downstream consumer, allowing for scalability, and easier refactor when you decide to move Pub-Sub to a separate service.&lt;/p&gt;
&lt;p&gt;Rocketman also works without Rails.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Installation&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Add this line to your application's Gemfile:&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-en"&gt;gem&lt;/span&gt; &lt;span class="pl-s"&gt;'rocketman'&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And then execute:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ bundle
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or install it yourself as:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ gem install rocketman
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Usage&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Rocketman exposes two module, &lt;code&gt;Rocketman::Producer&lt;/code&gt; and &lt;code&gt;Rocketman::Consumer&lt;/code&gt;. They do exactly as what their…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/edisonywh/rocketman" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;emitting events look as easy as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="ss"&gt;:hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;payload: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"one"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"two"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and listening for events look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;on_event&lt;/span&gt; &lt;span class="ss"&gt;:hello&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;payload&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s more information on the &lt;code&gt;Usage&lt;/code&gt; section and &lt;code&gt;Roadmap&lt;/code&gt; if you’re interested in the project, otherwise, feel free to comment here too!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How do you do API versioning?</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Wed, 24 Jul 2019 13:19:08 +0000</pubDate>
      <link>https://dev.to/edisonywh/how-do-you-do-api-versioning-5bkd</link>
      <guid>https://dev.to/edisonywh/how-do-you-do-api-versioning-5bkd</guid>
      <description>&lt;p&gt;Recently been thinking how to do API versionings, and realised there's multiple ways of doing things. I do not think there's only &lt;em&gt;one right way&lt;/em&gt; , but I am really curious to hear what you people have to add!&lt;/p&gt;

&lt;p&gt;There's mainly a couple of questions I'm wondering (I wish dev.to has the polls features out already!)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do you prefix the version to the resource, or suffix it to the resource? e.g: &lt;code&gt;/v1/users/&lt;/code&gt; or &lt;code&gt;/users/v1/&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;When you have a breaking change for a specific action (&lt;code&gt;POST /users&lt;/code&gt; now take different params), do you bump the whole &lt;code&gt;/users&lt;/code&gt; version, or do you bump that specific &lt;code&gt;POST /users&lt;/code&gt; action?&lt;/li&gt;
&lt;li&gt;Where do you keep the serializers? in the same folder, or different?&lt;/li&gt;
&lt;li&gt;APIs are usually backed up by business logics, in some cases being called Service Objects in Rubyland — do you guys also bump your Services when your API is updated?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more concrete example, let's say you have an API to reset password for user, eg: &lt;code&gt;/v1/users/:id/reset_password&lt;/code&gt;, then for some reason you have a breaking change for &lt;code&gt;/reset_password&lt;/code&gt;. What do you do now?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bump the entire resource to &lt;code&gt;v2&lt;/code&gt; (e.g: &lt;code&gt;/v2/users&lt;/code&gt;, &lt;code&gt;/v2/users/:id&lt;/code&gt;,
&lt;code&gt;/v2/users/:id/reset_password&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Bump only specific action (e.g: &lt;code&gt;/v1/users, /v1/users/:id, /v2/users/:id/reset_password&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Rethink versioning to scope by action instead (e.g: &lt;code&gt;/users/v2/:id/reset_password&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my opinion, this makes sense to me:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api/
 |&amp;gt; users/
     |&amp;gt; v1/
        |&amp;gt; api.rb
        |&amp;gt; serializer.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Couple of things I'm doing here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I suffix the version to resource, not prefix (breaks REST)&lt;/li&gt;
&lt;li&gt;I place serializers and endpoint in the same file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So whenever there's a breaking change in API (e.g: parameter changes), I think it logically makes more sense to bump the whole resource even if it's just one action, reason being as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deprecation&lt;/strong&gt; is easier - if you know that &lt;code&gt;/v1&lt;/code&gt; endpoint is not being used anymore (through your APM), then just delete off the whole folder itself without worrying it'll impact other part of the app. No need to ask questions like: "v1 is still in use &lt;em&gt;except&lt;/em&gt; for this specific action, for that it'll be in v2"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Locality&lt;/strong&gt; - I think that inherently serializers are just a way to serialize your data into JSON, and so it should be versioned the same way as your API. By putting them in the same folder, it ensures that you're &lt;strong&gt;more likely to update both of them at the same time&lt;/strong&gt;, and it also makes it &lt;strong&gt;much easier for developers to see which API is using which serializer&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what do you guys think/do currently?&lt;/p&gt;

</description>
      <category>healthydebate</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Introducing `Behaves`, a better way to define contracts/behaviors between classes</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Fri, 07 Jun 2019 15:30:54 +0000</pubDate>
      <link>https://dev.to/edisonywh/introducing-behaves-a-better-way-to-define-contracts-behaviors-between-classes-1el8</link>
      <guid>https://dev.to/edisonywh/introducing-behaves-a-better-way-to-define-contracts-behaviors-between-classes-1el8</guid>
      <description>&lt;p&gt;Hey guys!&lt;/p&gt;

&lt;p&gt;I just published my first gem on RubyGems, and would love to hear your feedback!&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/edisonywh" rel="noopener noreferrer"&gt;
        edisonywh
      &lt;/a&gt; / &lt;a href="https://github.com/edisonywh/behaves" rel="noopener noreferrer"&gt;
        behaves
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Define behaviors and contracts between your code.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/9ef73e34f50bacd2f2d6e1e5d420331dadd3489b5693136ca944b55f7293d9b1/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f6275696c642f6769746875622f656469736f6e7977682f626568617665732e737667"&gt;&lt;img src="https://camo.githubusercontent.com/9ef73e34f50bacd2f2d6e1e5d420331dadd3489b5693136ca944b55f7293d9b1/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f6275696c642f6769746875622f656469736f6e7977682f626568617665732e737667" alt="CircleCI Badge"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/b613fd14d4ae8ab010c506e7a3d6f87734e01215be08b065ce787877c4e4809b/68747470733a2f2f696d672e736869656c64732e696f2f67656d2f762f626568617665732e737667"&gt;&lt;img src="https://camo.githubusercontent.com/b613fd14d4ae8ab010c506e7a3d6f87734e01215be08b065ce787877c4e4809b/68747470733a2f2f696d672e736869656c64732e696f2f67656d2f762f626568617665732e737667" alt="RubyGems Badge"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/6c4a6035fd5e324fb0607f079ebf320e4c88adb36c6e905754b4808de9205406/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f656469736f6e7977682f626568617665732e737667"&gt;&lt;img src="https://camo.githubusercontent.com/6c4a6035fd5e324fb0607f079ebf320e4c88adb36c6e905754b4808de9205406/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f656469736f6e7977682f626568617665732e737667" alt="Code Climate maintainability"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Behaves&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;Behaves is a gem that helps you define behaviors between classes. &lt;strong&gt;Say goodbye to runtime error when defining behaviors.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Behaves is especially useful for dealing with adapter patterns by making sure that all of your adapters define the required behaviors. See &lt;a href="https://github.com/edisonywh/behaves#usage" rel="noopener noreferrer"&gt;usage below&lt;/a&gt; for more examples.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Detailed explanations in the sections below.&lt;/em&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Installation&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Add this line to your application's Gemfile:&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-en"&gt;gem&lt;/span&gt; &lt;span class="pl-s"&gt;'behaves'&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Usage&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;This is how you define behaviors with &lt;code&gt;behaves&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, define required methods on the &lt;code&gt;Behavior Object&lt;/code&gt; with the &lt;code&gt;implements&lt;/code&gt; method, which take a list of methods.&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;class&lt;/span&gt; &lt;span class="pl-v"&gt;Animal&lt;/span&gt;
  &lt;span class="pl-en"&gt;extend&lt;/span&gt; &lt;span class="pl-v"&gt;Behaves&lt;/span&gt;

  &lt;span class="pl-en"&gt;implements&lt;/span&gt; &lt;span class="pl-pds"&gt;:speak&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-pds"&gt;:eat&lt;/span&gt;
&lt;span class="pl-k"&gt;end&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then, you can turn any object (the &lt;code&gt;Behaving Object&lt;/code&gt;) to behave like the &lt;code&gt;Behavior Object&lt;/code&gt; by using the &lt;code&gt;behaves_like&lt;/code&gt; method, which takes a &lt;code&gt;Behavior Object&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;class&lt;/span&gt; &lt;span class="pl-v"&gt;Dog&lt;/span&gt;
  &lt;span class="pl-en"&gt;extend&lt;/span&gt; &lt;span class="pl-v"&gt;Behaves&lt;/span&gt;

  &lt;span class="pl-en"&gt;behaves_like&lt;/span&gt; &lt;span class="pl-v"&gt;Animal&lt;/span&gt;
&lt;span class="pl-k"&gt;end&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Voilà, that's all it takes to define behaviors! Now if &lt;code&gt;Dog&lt;/code&gt; does not implement &lt;code&gt;speak&lt;/code&gt; and…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/edisonywh/behaves" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;Behaves is a gem that helps you maintain contracts between different classes. This is especially useful for dealing for adapter patterns by making sure that all of your adapters define the required behaviors.&lt;/p&gt;

&lt;p&gt;The idea for &lt;code&gt;Behaves&lt;/code&gt; stemmed from my research into &lt;a href="https://www.sitepoint.com/using-and-testing-the-adapter-design-pattern/" rel="noopener noreferrer"&gt;adapter pattern in Ruby&lt;/a&gt; and José Valim's article on &lt;a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/" rel="noopener noreferrer"&gt;Mocks and explicit contracts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I found that the current idiom to achieve &lt;code&gt;behaviors&lt;/code&gt; in Ruby is through &lt;code&gt;Inheritence&lt;/code&gt;, and then subsequently defining a "required" (I put quotation marks around it, because it's not exactly &lt;code&gt;required&lt;/code&gt; until you run it) method, which does nothing except raising a &lt;code&gt;NotImplementedError&lt;/code&gt;. While I don't necessarily think it's &lt;em&gt;bad&lt;/em&gt;, I do think that there could be an alternative that's &lt;strong&gt;more explicit&lt;/strong&gt;, &lt;strong&gt;less boilerplate&lt;/strong&gt;, &lt;strong&gt;cleaner ancestors hierachy&lt;/strong&gt;, thus the birth of &lt;code&gt;Behaves&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Best of all? &lt;strong&gt;No more runtime errors for defining behaviors!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Do let me know what you think!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>opensource</category>
      <category>showdev</category>
    </item>
    <item>
      <title>A Tale of Curiosity at work - work doesn't have to be boring</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Sat, 01 Jun 2019 06:45:00 +0000</pubDate>
      <link>https://dev.to/edisonywh/a-tale-of-curiosity-at-work-work-doesn-t-have-to-be-boring-2122</link>
      <guid>https://dev.to/edisonywh/a-tale-of-curiosity-at-work-work-doesn-t-have-to-be-boring-2122</guid>
      <description>&lt;p&gt;Well my usual articles are dead boring so today I'm going to switch it up a bit. Today I'm going to talk about, a day at work. Or if you will, &lt;em&gt;A Tale of Curiosity&lt;/em&gt;. I promise it'll be fun, or at least I'll try to make it so.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3KCOFfdqmptLi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3KCOFfdqmptLi/giphy.gif" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently my team and I have been integrating with a third-party API, and I was tasked to translate the error returned by the third-party API into something that we can show to our user.&lt;/p&gt;

&lt;p&gt;For example, if the third-party API responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Error message from third-party"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do not necessarily want to render their message to our users, because it could very well be some random error that user would not understand (e.g &lt;code&gt;Required argument is not passed in&lt;/code&gt;), also the fact that by mapping their error codes to our own allows us to control the tone and voice of our company. So, time to work!&lt;/p&gt;

&lt;p&gt;After a bit, I was done with my task and I sent it off to a co-worker for review, like hooray, time for the next task.&lt;/p&gt;

&lt;p&gt;He came back to me in 5 minutes and asked: &lt;strong&gt;&lt;em&gt;why did you do it this way&lt;/em&gt;&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;The pull request in question looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_tHd9sFs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jum31cohz2djhixg6798.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_tHd9sFs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jum31cohz2djhixg6798.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In fact, he was specifically asking me why did I choose to memoize my &lt;code&gt;error_hash&lt;/code&gt; method?&lt;/p&gt;

&lt;p&gt;The conversation went like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;C: Hey why did you memoize this hash?&lt;br&gt;
Me: Well, we're going to be accessing this rather often, wouldn't want to recalculate it again and again since this value is not going to change.&lt;br&gt;
C: But you only really memoize for expensive computation, such as database calls, API calls etc.&lt;br&gt;
Me: That makes sense, but I want every single call to &lt;code&gt;error_hash&lt;/code&gt; to return me the same object, so it's more memory efficient that way.&lt;br&gt;
C: I don't really see that as a good reason. And is that really how it works?&lt;br&gt;
Me: Honestly I'm not quite sure, I just sort of assumed that. Can I get like 10 minutes to run a benchmark to figure it out?&lt;br&gt;
C: You bet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I decided to do a quick benchmark to see if my hypothesis was right (and because I'm going to prove that little sucker wrong): that memoizing will return me the exact same object in memory, while if I don't, a new Hash gets generated every time.&lt;/p&gt;

&lt;p&gt;Since this is a case of interacting with the Ruby memory, I turned to my best friend &lt;code&gt;ObjectSpace&lt;/code&gt; (whom I've mentioned in a couple of my article). &lt;code&gt;ObjectSpace&lt;/code&gt; basically allows you to interact with the living objects at runtime. Perfect fit for us!&lt;/p&gt;

&lt;p&gt;I took about 10 mins to whip up &lt;a href="https://gist.github.com/edisonywh/32ddf65865cc1c91eeedfa929a2d0bec"&gt;this quick script&lt;/a&gt;. You can take a look at the code if you'd wish, but basically what I had hypothesized was accurate, that if I don't memoize it'd generate a lot of extra objects in memory.&lt;/p&gt;

&lt;p&gt;However, did I go with my approach? Nope. He then asked me why didn't I just used a constant, and I realized that's exactly what I wanted, so I've updated my solution to use a constant instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;p&gt;Well if there's one thing that I hope you can take away from this is that you should &lt;strong&gt;be open to learning/feedback&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a look at this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My colleague has questioned me, not in the tone of superiority, but genuine &lt;strong&gt;curiosity&lt;/strong&gt; as to why I did what I did.&lt;/li&gt;
&lt;li&gt;I explained why I did and he did not really agree with it, but we decided that I can run some tests to back up my claim.&lt;/li&gt;
&lt;li&gt;I did exactly that, and now we have more data to be used in our judgement.&lt;/li&gt;
&lt;li&gt;We both assessed the new information and decided that there's a better way (constant), and we both chose to let down our previously held opinion and go with what's the best solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This little interaction took maybe about tops of 15 minutes? And within 15 minutes we were able to learn more about Ruby internals and go home with more knowledge.&lt;/p&gt;

&lt;p&gt;What looked like a mundane task at first had just turned into an interesting learning session, and it only happened because we were both curious. &lt;strong&gt;Trust me, curiosity doesn't kill the cat, let loose and let your curiosity roam free!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope by reading this, I had encouraged you to hold the mind of a curious and ask yourself, is there something I can learn from this, no matter how mundane the task looks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you have any similar story where you managed to turn a mundane task into a great learning opportunity, feel free to share them down in the comments!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Right, better head back to work now before my manager taps on my shoulder. (&lt;em&gt;well he doesn't really do that&lt;/em&gt;..)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Function/Method look up in Elixir/Ruby</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Thu, 21 Mar 2019 10:31:17 +0000</pubDate>
      <link>https://dev.to/edisonywh/functionmethod-look-up-in-elixirruby-622</link>
      <guid>https://dev.to/edisonywh/functionmethod-look-up-in-elixirruby-622</guid>
      <description>&lt;p&gt;So to preface, I'm a Ruby dev learning Elixir. I love to compare what I'm learning in Elixir back to what I know in Ruby, as I think that really strengthens my understanding of both languages. Today, I want to talk about function/method look up.&lt;/p&gt;

&lt;p&gt;This article was prompted when I was learning about &lt;a href="https://hexdocs.pm/elixir/GenServer.html" rel="noopener noreferrer"&gt;&lt;code&gt;GenServer&lt;/code&gt;&lt;/a&gt; through &lt;a href="https://pragmaticstudio.com/courses/elixir" rel="noopener noreferrer"&gt;PragmaticStudio's course&lt;/a&gt; (I highly recommend it), and they mentioned that &lt;code&gt;GenServer&lt;/code&gt; can inject default implementations of functions if you do not define them manually. These default functions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;init/1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;child_spec/1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;code_change/3&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;terminate/2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;handle_info/2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;handle_cast/2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;handle_call/3&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And if you want to override them, just define a function in the current module with the same signature (name + arity).&lt;/p&gt;

&lt;p&gt;But this got me thinking, &lt;em&gt;how&lt;/em&gt; is it that Elixir look up function definition that makes this possible?&lt;/p&gt;

&lt;p&gt;Before we jump into Elixir, let's first look at how method lookup in Ruby works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your dad, your dad's dad, and your dad's dad's dad.
&lt;/h2&gt;

&lt;p&gt;I have actually blogged about this before, which was &lt;a href="https://twitter.com/yukihiro_matz/status/1069401210162016256" rel="noopener noreferrer"&gt;retweeted by Matz&lt;/a&gt; himself! (I actually have no idea how much this actually means, but I'm still going to wear it like a badge of honor)&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/edisonywh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg" alt="edisonywh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/edisonywh/ruby-has-no-class-methods-39l5" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Ruby has no class methods&lt;/h2&gt;
      &lt;h3&gt;Edison Yap ・ Nov 9 '18&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#rails&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Ruby's method lookup chain can literally be summarized in one method call - &lt;code&gt;ancestors&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;# Lookup path for instance methods&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; [Foo, Object, Kernel, BasicObject]&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;# Lookup path for class (singleton) methods, read my blog to learn more!&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; [Class, Module, Object, Kernel, BasicObject] &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it -- that's how you see the entire method lookup chain for Ruby!&lt;/p&gt;

&lt;p&gt;In fact, &lt;code&gt;#ancestors&lt;/code&gt; is defined on &lt;code&gt;BasicObject&lt;/code&gt; itself, so when you call &lt;code&gt;Foo.ancestors&lt;/code&gt;, it asks this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;You: Do you implement `#ancestors`?
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gd"&gt;- Foo: No
- Object: No
- Kernel: No
&lt;/span&gt;&lt;span class="gi"&gt;+ BasicObject: Yes I do!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's how it works! &lt;/p&gt;

&lt;p&gt;With that, we know that if we want to &lt;code&gt;inject&lt;/code&gt; a function into Ruby, we just have to make sure that the newly injected function is the first method that gets found during the lookup (defining &lt;code&gt;#ancestors&lt;/code&gt; anywhere before &lt;code&gt;BasicObject&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;So, Ruby is able to do this because of &lt;code&gt;ancestors&lt;/code&gt;, but Elixir doesn't really have the concept of &lt;code&gt;ancestors&lt;/code&gt;/&lt;code&gt;inheritance&lt;/code&gt;, so what is really happening here?&lt;/p&gt;

&lt;p&gt;It's about time to stop guessing, so I decided to try out an example myself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Parent&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;__using__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;injected&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="s2"&gt;"This is from Parent"&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;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Child&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Parent&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;injected&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="s2"&gt;"This is from Child"&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;My expected behavior is that my &lt;code&gt;Parent.injected/0&lt;/code&gt; will be overriden by my &lt;code&gt;Child.injected/0&lt;/code&gt;, but when I try to run this file - &lt;code&gt;iex lookup.exs&lt;/code&gt;, the following warning message pops up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;warning: this clause cannot match because a previous clause at line &lt;strong&gt;12&lt;/strong&gt; always matches lookup.exs:&lt;strong&gt;14&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Weird warning, but let's try to actually run it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Child&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;injected&lt;/span&gt;
&lt;span class="s2"&gt;"This is from Parent"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To my surprise (well the surprise is kinda ruined when I saw the warning message), running &lt;code&gt;Child.injected&lt;/code&gt; returns me &lt;code&gt;Parent.injected/0&lt;/code&gt; instead of &lt;code&gt;Child.injected/0&lt;/code&gt;, why?!&lt;/p&gt;

&lt;p&gt;I decided to &lt;a href="https://hexdocs.pm/elixir/GenServer.html" rel="noopener noreferrer"&gt;dig into the doc&lt;/a&gt; and find out why, but the docs isn't really tell me much apart from the fact that &lt;code&gt;GenServer&lt;/code&gt; &lt;em&gt;will&lt;/em&gt; inject functions for you. But I already knew that, I just want to know &lt;em&gt;how&lt;/em&gt; is it doing so.&lt;/p&gt;

&lt;p&gt;So, docs didn't help, what next?&lt;/p&gt;

&lt;h2&gt;
  
  
  Source Code!
&lt;/h2&gt;

&lt;p&gt;As like most other languages, Elixir is open source, so I could very easily dive into the &lt;a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/gen_server.ex" rel="noopener noreferrer"&gt;source code itself&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now I see something &lt;a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/gen_server.ex#L818" rel="noopener noreferrer"&gt;suspiciously named&lt;/a&gt; &lt;code&gt;defoverridable&lt;/code&gt;, and I have a hunch that this might be it, but I want further proof to my hypothesis.&lt;/p&gt;

&lt;p&gt;A quick Google about &lt;code&gt;defoverridable&lt;/code&gt; sent me to a post in &lt;a href="https://elixirforum.com/t/behaviours-defoverridable-and-implementations/3338" rel="noopener noreferrer"&gt;Elixir Forum&lt;/a&gt;, written by the man himself, José Valim.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;So through that post I found out that I can use &lt;code&gt;defoverridable&lt;/code&gt;, which would mean that I can update my code to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;defmodule Parent do
&lt;/span&gt;  defmacro __using__(_opts) do
    quote do
      def injected do
        "This is from Parent"
      end
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+     defoverridable injected: 0
&lt;/span&gt;    end
  end
&lt;span class="p"&gt;end
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;defmodule Child do
&lt;/span&gt;  use Parent
&lt;span class="err"&gt;
&lt;/span&gt;  def injected do
    "This is from Child"
  end
&lt;span class="p"&gt;end
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now if we run it..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Child&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;injected&lt;/span&gt;
&lt;span class="s2"&gt;"This is from Child"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà it works! Perfect 🎉&lt;/p&gt;

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

&lt;p&gt;In his post, José clarified &lt;code&gt;defoverridable&lt;/code&gt; for me, but he also went on to say that &lt;code&gt;defoverridable&lt;/code&gt; is &lt;strong&gt;not recommended&lt;/strong&gt;, and they are in fact moving towards to &lt;code&gt;@optional_callback&lt;/code&gt; to mark methods as optional, along with an enhancement &lt;code&gt;@impl&lt;/code&gt; (which the compiler will use to make sure "child" implements that function), so that was also an interesting thing that I learned. I recommend you to read the post and comments to understand it a little more.&lt;/p&gt;

&lt;p&gt;My query was literally answered in the first 10 lines of his post, so it &lt;em&gt;would've been really nice&lt;/em&gt; if I managed to find it in the first place. Then again, if I hadn't went into the source code I wouldn't have known what keyword to look for, and thus won't land at that post.&lt;/p&gt;

&lt;p&gt;Still, an interesting journey for me because &lt;em&gt;at one point I thought Elixir had inheritance like Ruby, heh.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(also, title is technically incorrect as Elixir does not really have "function lookup" - all it does is just look at the current module!)&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>ruby</category>
      <category>beginners</category>
      <category>functional</category>
    </item>
    <item>
      <title>Elixir's If and Elixir's Do</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Tue, 12 Mar 2019 15:50:57 +0000</pubDate>
      <link>https://dev.to/edisonywh/elixirs-if-and-elixirs-do-jol</link>
      <guid>https://dev.to/edisonywh/elixirs-if-and-elixirs-do-jol</guid>
      <description>&lt;p&gt;So recently I came across the fact that Elixir is full of macros, and that it allows you to interact with its internal syntax tree pretty easily (using &lt;code&gt;quote&lt;/code&gt;), and I thought that was a pretty cool thing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the simplest of terms macros are special functions designed to return a quoted expression that will be inserted into our application code. -- &lt;a href="https://elixirschool.com/en/lessons/advanced/metaprogramming/#macros" rel="noopener noreferrer"&gt;Elixir Lang&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One example that I came across was the following -- say we have an expression that we want to evaluate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But sometimes you want to write a single-line expression. &lt;em&gt;No problemo amigo&lt;/em&gt;, Elixir's got you covered!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This statement is a single line long, but &lt;strong&gt;it excites me enough to write a post about it.&lt;/strong&gt; Let's dive into why!&lt;/p&gt;

&lt;p&gt;There are &lt;strong&gt;three&lt;/strong&gt; things to note here in this one, simple expression.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There's a &lt;strong&gt;comma&lt;/strong&gt; &lt;em&gt;before&lt;/em&gt; &lt;code&gt;do&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;There's a &lt;strong&gt;colon&lt;/strong&gt; &lt;em&gt;after&lt;/em&gt; do&lt;/li&gt;
&lt;li&gt;There's &lt;strong&gt;no&lt;/strong&gt; &lt;code&gt;end&lt;/code&gt; keyword&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three are &lt;em&gt;hints&lt;/em&gt; to a reveal that blew my mind.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement you just saw? It's not really a language keyword, it's just a normal function (in fact, it's a macro).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;do&lt;/code&gt; block you just saw? Not a &lt;em&gt;language&lt;/em&gt; keyword either, it's a keyword list (well technically it's a keyword list keyword according to &lt;a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/macro.ex#L839" rel="noopener noreferrer"&gt;source code&lt;/a&gt;). If you don't know what a keyword list is, keep reading, I explain it a little further down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthumbs.gfycat.com%2FKlutzyIndolentGander-max-1mb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthumbs.gfycat.com%2FKlutzyIndolentGander-max-1mb.gif" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How did the hints help?
&lt;/h2&gt;

&lt;p&gt;Let's revisit the hints and see how the hints help us uncover the truth which has been hiding in plain sight all along!&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Comma
&lt;/h3&gt;

&lt;p&gt;If you paid attention earlier, the first hint points out that &lt;code&gt;true&lt;/code&gt; and the &lt;code&gt;do&lt;/code&gt; statement are separated.&lt;/p&gt;

&lt;p&gt;Do you know what else is separated? &lt;strong&gt;Function arguments&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;if&lt;/code&gt; statement is really just a function that has arity of 2, first being the condition to evaluate, second being the action to do, and &lt;strong&gt;the comma separates the two arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This means that we can translate our earlier statement to look like this, and it'll still work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All I did was add brackets around it - don't they look a lot more familiar now? ;)&lt;/p&gt;

&lt;h3&gt;
  
  
  2) Colon
&lt;/h3&gt;

&lt;p&gt;The second and third hints are actually very closely related, because they both point to one thing: &lt;strong&gt;keyword list&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a refresher on what a keyword list is, or what it looks like.&lt;/p&gt;

&lt;p&gt;Official definition on &lt;code&gt;Keyword List&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A keyword is a &lt;strong&gt;list of two-element tuples&lt;/strong&gt; where the first element of the tuple is an atom and the second element can be any value. - &lt;a href="https://hexdocs.pm/elixir/Keyword.html" rel="noopener noreferrer"&gt;Elixir Lang&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We know what a list looks like -&amp;gt;&lt;code&gt;[]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We know what a tuple looks like -&amp;gt; &lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, a keyword list looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:atom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"any_value"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright that doesn't look very nice/easy to type out, so Elixir provides a syntactic sugar to write a keyword list like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;atom:&lt;/span&gt; &lt;span class="s2"&gt;"any_value"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, we can use this newfound logic to apply back to our previous &lt;code&gt;if&lt;/code&gt; statement, and that translates to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, all I did here was add square brackets around the &lt;code&gt;do&lt;/code&gt; keyword, and it's beginning to clear up what they are.&lt;/p&gt;

&lt;p&gt;In fact, we could get a little crazier with the original syntax of keyword list..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:do&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt;
&lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Hooray, still works!&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3) Where's the &lt;code&gt;end&lt;/code&gt;? (and proofy time!)
&lt;/h3&gt;

&lt;p&gt;Alright, so I've told you that they're the same thing and that they all work exactly the same, but as far as you're concerned I could be blabbering random shits (&lt;em&gt;Although I promise I'm not&lt;/em&gt;). So let's prove that what I said was right!&lt;/p&gt;

&lt;p&gt;I have also yet to tell you about the 3rd hint, which is - where did the &lt;code&gt;end&lt;/code&gt; keyword go?&lt;/p&gt;

&lt;p&gt;So, the &lt;code&gt;end&lt;/code&gt; is actually completely ignored because there's no real need to represent an &lt;code&gt;end&lt;/code&gt; of statement in an Abstract Synax Tree -- you should be able to infer it from the syntax tree itself. I also show evidence of this in Elixir's source code later on.&lt;/p&gt;

&lt;p&gt;I won't talk too much about AST partially because it's a whole another topic on its own, but also because &lt;em&gt;I honestly also don't know enough about it&lt;/em&gt; (😬) but a quick primer/what I understand so far here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AST is how the machine understand the flow of code. It builds a tree based on the programming languages' syntax and how machine execution should flow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In an essence, &lt;strong&gt;you write a block of text (the code), and the language tries to understand it (parses), and then it creates a mental model of what it currently understands (the abstract syntax tree).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reason why I'm running the different expressions to produce an AST to prove that they're the same is because, the same-ness of the code isn't depended on any syntactic sugar, but rather what the language really understands from it, and in our case, it's the mental model (AST) of the language.&lt;/p&gt;

&lt;p&gt;Time to verify!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="s2"&gt;"hello world"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# {:if, [context: Elixir, import: Kernel], [true, [do: "hello world"]]}&lt;/span&gt;

&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# {:if, [context: Elixir, import: Kernel], [true, [do: "hello world"]]}&lt;/span&gt;

&lt;span class="n"&gt;third&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# {:if, [context: Elixir, import: Kernel], [true, [do: "hello world"]]}&lt;/span&gt;

&lt;span class="n"&gt;fourth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# {:if, [context: Elixir, import: Kernel], [true, [do: "hello world"]]}&lt;/span&gt;

&lt;span class="n"&gt;fifth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&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;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:do&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# {:if, [context: Elixir, import: Kernel], [true, [do: "hello world"]]}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We can run a final check to check if they all equal to each other!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;first&lt;/span&gt;  &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;  &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="n"&gt;third&lt;/span&gt;  &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fourth&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="n"&gt;fourth&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fifth&lt;/span&gt;  &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll be frank, I don't know how Elixir actually decides to continue reading/parsing the statement until an &lt;code&gt;end&lt;/code&gt; is met, I feel like there's a whole another topic of lexing, parsing and tokenizing that I'll need to understand first (along with AST, which I will, but not yet!)&lt;/p&gt;

&lt;p&gt;BUT! What I can add to support this article is the evidence that when Elixir outputs the AST back as a string (with &lt;code&gt;Macro.to_string/1&lt;/code&gt;), they have to explicitly append the &lt;code&gt;end&lt;/code&gt; word into the outputted string, &lt;a href="https://github.com/elixir-lang/elixir/blob/0903ad17bc5631a540d0c76d2632baae15319158/lib/elixir/lib/macro.ex#L1033" rel="noopener noreferrer"&gt;as seen from here&lt;/a&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's two things that I learned from just this one simple line of code, and that is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;if&lt;/code&gt; is just a language macro (it expands down to a &lt;code&gt;case&lt;/code&gt; statement), and in fact, there's a whole new world about meteprogramming/macros in Elixir's world!&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;do&lt;/code&gt; is nothing more than a keyword list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had thought that basic things like &lt;code&gt;if&lt;/code&gt; statements or declaring a function should be core language features with special meanings, but Elixir is able to beautifully build upon fundamental features like AST/keyword list and use that to build out other features.&lt;/p&gt;

&lt;p&gt;This also means you can build any sort of macro that you want! Want to build an &lt;code&gt;unless&lt;/code&gt; feature like in Ruby? You can do that (as shown in &lt;a href="https://elixir-lang.org/getting-started/meta/macros.html#our-first-macro" rel="noopener noreferrer"&gt;Elixir official guide&lt;/a&gt;), want to build a &lt;code&gt;while&lt;/code&gt; loop?, you can do that too (Chris McCord shows this in his &lt;a href="https://www.youtube.com/watch?v=2Bjzml_Hpvk" rel="noopener noreferrer"&gt;introductory video to Metaprogramming Elixir&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In fact.... let me just leave you with one thing. &lt;code&gt;def&lt;/code&gt; itself is a macro, defined using &lt;code&gt;defmacro&lt;/code&gt;. And &lt;code&gt;defmacro&lt;/code&gt; itself is a macro, defined using &lt;code&gt;defmacro&lt;/code&gt;. Mind blown yet? If not feel free to re-read that sentence until you do. &lt;a href="https://elixirschool.com/en/lessons/advanced/metaprogramming/" rel="noopener noreferrer"&gt;Read more here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Author's note
&lt;/h2&gt;

&lt;p&gt;I've probably glossed over a lot of details (particularly about AST) because frankly, I do not understand them well enough yet (and the point of the article is not really on AST, they're just a means to proving they're all equal without the syntactic sugars).&lt;/p&gt;

&lt;p&gt;But this means I know exactly what I'll be diving into next, and I can't wait to learn more about lexers, parsers, tokenizers and ASTs :) If you have any good resources on them, comment down below ⬇️, I'd really appreciate them!&lt;/p&gt;

&lt;p&gt;P.S Credit where credit's due, I learned about this from the Elixir Slack channel &lt;a href="https://elixir-slackin.herokuapp.com/" rel="noopener noreferrer"&gt;over here&lt;/a&gt;, &lt;strong&gt;Martin Svalin&lt;/strong&gt; was explaining that they're all equivalent, and I decided to dive a little deeper and explore the AST to verify what he's saying. &lt;em&gt;Spoiler: he's right!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>beginners</category>
      <category>functional</category>
    </item>
    <item>
      <title>[ Elixir | Why Linked Lists? ]</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Tue, 12 Feb 2019 13:00:02 +0000</pubDate>
      <link>https://dev.to/edisonywh/-elixir--why-linked-lists--1e9d</link>
      <guid>https://dev.to/edisonywh/-elixir--why-linked-lists--1e9d</guid>
      <description>&lt;p&gt;I've always thought data structures are cool, but you know what's cooler? Seeing them in the wild!&lt;/p&gt;

&lt;p&gt;While going through Elixir's doc, I saw that Elixir uses linked lists under the hood for their linear data structure. I thought this was cool, but something struck me; I understood arrays and linked lists, but I had no idea how it relates to programming languages and it's bothered me ever since and I &lt;em&gt;need&lt;/em&gt; to find out &lt;em&gt;why&lt;/em&gt; linked list was used, hence this article!&lt;/p&gt;

&lt;p&gt;So back to the article, from what I've found so far, there are three reasons as to why Elixir does this (and I could be totally wrong, feel free to correct me!). Let's go through one by one:&lt;/p&gt;

&lt;h2&gt;
  
  
  Immutable Data
&lt;/h2&gt;

&lt;p&gt;In Elixir, (most functional languages actually), data are immutable. This is an important first step to understanding why linked lists is used, so let's explore this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutable means that once a data is declared, it &lt;em&gt;cannot&lt;/em&gt; be mutated/changed anymore.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Assuming you know how arrays work under the hood (check out &lt;a href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg"&gt;my other article&lt;/a&gt; if you want a refresher). Let's take a look at what happens if we try to implement immutability with an array!&lt;/p&gt;

&lt;p&gt;Array is defined as a continous block of memory. The problem with this is that, an array of 5 elements is still just ONE array, and when we are adding/deleting an element, we are MUTATING it. How can we use immutability with arrays then? It's not impossible, but let's look at why that's not practical.&lt;/p&gt;

&lt;p&gt;If we want to enforce true immutability in arrays, this means that we need to make a full copy of the old array everytime we want to add/delete in the array.&lt;/p&gt;

&lt;p&gt;Which means, if you have an array of size 5, if you want to add a new item to the array, your memory usage is instantly doubled (because you need to keep the old array as is, and you also need to make a new array of same elements). And that's just space complexity, there's also time complexity that we need to think about!&lt;/p&gt;

&lt;p&gt;A linked list doesn't have the same constraint, as the nodes are all stored separately in memory, which means we don't really need to worry about space/time complexity while adding/delete nodes in list.&lt;/p&gt;

&lt;p&gt;This gives us our first reason as to why it uses a list, however that's not the whole story - here's where recursive structural/tail sharing jumps in and everything starts making sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recursive structure
&lt;/h2&gt;

&lt;p&gt;Did you notice that linked lists are recursive by definition?&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt; is a linked list, but so is &lt;code&gt;B -&amp;gt; C -&amp;gt; D&lt;/code&gt;, &lt;code&gt;C -&amp;gt; D&lt;/code&gt; and so on, and each linked list is just a &lt;em&gt;sub&lt;/em&gt; structure of another linked list!&lt;/p&gt;

&lt;p&gt;Well that wasn't very exciting on its own, but this is vital to the next piece of puzzle!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Fun Fact: The recursive nature coupled with the fact that datas have to be immutable (so you can't have a loop counter) is why functional languages are usually associated with recursions - they kinda have to!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Structural/Tail Sharing
&lt;/h2&gt;

&lt;p&gt;So, we know linked lists are recursive in nature. Combined with the immutable nature of the language, we know that the data can never change.&lt;/p&gt;

&lt;p&gt;This is interesting, because now we can confidently say that &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt; is a different list from &lt;code&gt;B -&amp;gt; C -&amp;gt; D&lt;/code&gt; (even though one recursively contains the other one), and because we have that guarantee (along with the fact that a list CAN'T change), we don't have to define the same datas twice, and &lt;strong&gt;we can reuse existing linked lists!&lt;/strong&gt; This is called &lt;strong&gt;Structural sharing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Awesome isn't it? Let's look at an example.&lt;/p&gt;

&lt;p&gt;e.g:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;list_one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;list_two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we have THREE different lists! &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;list_one&lt;/code&gt; and &lt;code&gt;list_two&lt;/code&gt;, but all of them share the same reference (the tail) and the only difference between them is the head pointer.&lt;/p&gt;

&lt;p&gt;This means that there will be a total of 6 elements in memory. Adding to list has low memory cost, while retaining the immutability that we desire.&lt;/p&gt;

&lt;p&gt;Reusable baby!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/fqygzR9D6j2uc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/fqygzR9D6j2uc/giphy.gif" alt="Baby smiling gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to read a little more, you can look into &lt;a href="https://en.wikipedia.org/wiki/Trie" rel="noopener noreferrer"&gt;Trie trees&lt;/a&gt; which have the exact same concepts of sharing datas/prefixes!&lt;/p&gt;
&lt;h3&gt;
  
  
  Garbage Collection &amp;amp; Caching?
&lt;/h3&gt;

&lt;p&gt;These two I'm not quite sure, but I've heard that linked lists are good for GCs and that tail sharing makes a good candidate for locality of reference/caching (I don't get how, because they aren't stored in the same places). Would appreciate if someone wants to chime in!&lt;/p&gt;
&lt;h2&gt;
  
  
  Closing Note
&lt;/h2&gt;

&lt;p&gt;Sidenote, in actuality it's not as much about Elixir since it compiles down to Erlang, but also not much about Erlang because all functional programming does pretty much same thing, but this is what prompted my curiousity hence the ties to Elixir.&lt;/p&gt;

&lt;p&gt;While writing this article, I found that I had to write in depth on how arrays work before I was able to dive into the Elixir part, so I've published that as &lt;a href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg"&gt;another article over here&lt;/a&gt; instead; do read that to gain a better understanding on what the tradeoff is!&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/edisonywh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg" alt="edisonywh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/edisonywh/how-arrays-work-the-way-arrays-work-3bpg" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How arrays work the way arrays work&lt;/h2&gt;
      &lt;h3&gt;Edison Yap ・ Feb 12 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#computerscience&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#c&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;I also did not really talk about Big O notations because I felt they might add unnecessary reading &lt;em&gt;time&lt;/em&gt; and &lt;em&gt;complexity&lt;/em&gt; to the article, but they're pretty vital and fundamental to computer science, so I suggest you brush up a little on them.&lt;/p&gt;

&lt;p&gt;If you're a podcast kind-of person, there's the &lt;a href="https://www.codenewbie.org/basecs" rel="noopener noreferrer"&gt;BaseCS&lt;/a&gt; by CodeNewbie, co-hosted by Vaidehi Joshi and Saron.&lt;/p&gt;

&lt;p&gt;If you want to read though, Vaidehi Joshi's blogpost version (which is what inspired the podcast I believe) is great too on &lt;a href="https://medium.com/basecs" rel="noopener noreferrer"&gt;BaseCS Medium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As for video, &lt;a href="https://www.youtube.com/playlist?list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P" rel="noopener noreferrer"&gt;MyCodeSchool&lt;/a&gt; is beyond amazing and is pratically where I learned everything that I know now, highly recommended!&lt;/p&gt;

&lt;p&gt;Other than that, hope you guys enjoyed the article as much as I enjoyed writing it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://elixir-lang.org/getting-started/basic-types.html#linked-lists" rel="noopener noreferrer"&gt;https://elixir-lang.org/getting-started/basic-types.html#linked-lists&lt;/a&gt; - The piece that prompted this article&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>computerscience</category>
      <category>functional</category>
    </item>
    <item>
      <title>How arrays work the way arrays work</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Tue, 12 Feb 2019 12:59:31 +0000</pubDate>
      <link>https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg</link>
      <guid>https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg</guid>
      <description>&lt;p&gt;In computer science, there is the concept of &lt;em&gt;linear data structure&lt;/em&gt;, which means that the datas are structured in a linear way in which &lt;strong&gt;&lt;em&gt;order matters&lt;/em&gt;&lt;/strong&gt;. There are &lt;strong&gt;Array&lt;/strong&gt; and &lt;strong&gt;Linked Lists&lt;/strong&gt;, but today I'll be talking mostly about arrays only, and a little about linked lists.&lt;/p&gt;

&lt;p&gt;Most &lt;em&gt;object-orientated languages&lt;/em&gt; come with &lt;strong&gt;Arrays&lt;/strong&gt;, whereas most &lt;em&gt;functional languages&lt;/em&gt; comes with &lt;strong&gt;Linked Lists&lt;/strong&gt; (see &lt;em&gt;why&lt;/em&gt; in another one of my article, mentioned at the bottom of the article).&lt;/p&gt;

&lt;p&gt;There's a good reason for this differentiation which we will dive into later, but for now let's just have a quick revision about what are the differences between the two data structures, and to know this, we'll need to take a trip down memory lane.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rewind Time
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc93wu6ylcorx3qmkbd3e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc93wu6ylcorx3qmkbd3e.gif" alt="Rewind time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Objects and functions and everything that we know about computers, are fundamentally stored in bits and bytes in computer.&lt;/p&gt;

&lt;p&gt;In languages like Java and C, you have to explicitly declare the size of an array beforehand. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hold up, but Ruby does not do that?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Ruby, we use &lt;code&gt;Array&lt;/code&gt; for our linear data structure, we can add seemingly infinite stuffs into an Ruby array and it would not matter as far as we are concerned.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&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;That's great isn't it?! That means arrays are &lt;em&gt;infinitely&lt;/em&gt; large right? And that Ruby is the superior language? Lucky us!&lt;/p&gt;

&lt;p&gt;But not so fast. &lt;em&gt;*pops your bubble*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are no &lt;em&gt;infinite size array&lt;/em&gt;, what you see in Ruby is what we call a &lt;em&gt;Dynamic Array&lt;/em&gt;, and it comes with a cost.&lt;/p&gt;

&lt;p&gt;To understand what dynamic arrays are, let's first take a look at how arrays are represented in memory. Since MRI Ruby (Matz' Ruby Interpreter) compiles down to C code, we'll look at how arrays are represented in C.&lt;/p&gt;
&lt;h3&gt;
  
  
  C-ing is believing
&lt;/h3&gt;

&lt;p&gt;We'll dive into a little bit of C code to help us C a little better.&lt;/p&gt;

&lt;p&gt;In lower level languages like C, you have to deal with pointers and memory allocation yourself, and even if you had not dealt with C before (&lt;em&gt;Disclaimer, neither have I&lt;/em&gt;), you may have C-een one of the most (in)famous example below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;

&lt;span class="cm"&gt;/* Example */&lt;/span&gt;
&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cast&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/* Usage */&lt;/span&gt;
&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;&lt;em&gt;taken from &lt;a href="https://www.programiz.com/c-programming/c-dynamic-memory-allocation" rel="noopener noreferrer"&gt;https://www.programiz.com/c-programming/c-dynamic-memory-allocation&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's break down this bits of code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;malloc&lt;/code&gt; doesn't have any magic meaning behind it, it literally stands for &lt;code&gt;memory allocation&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;malloc&lt;/code&gt; returns a pointer&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;malloc&lt;/code&gt; takes in an argument, which is the size the memory you want the program to allocate for you.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;100 * sizeof(int)&lt;/code&gt; tells the program that we want to store 100 integers, so allocate us 100 * the size of what each integer would occupy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ptr&lt;/code&gt;/pointer stores reference to the memory address.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Timmy stores luggage!
&lt;/h3&gt;

&lt;p&gt;If the above example did not really make sense, try this analogy. Think of memory allocation as like, a luggage concierge. It works like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timmy goes to the counter, tells the concierge that he have 2 pieces of luggages, about &lt;em&gt;this&lt;/em&gt; big, and that that he'd like to store it in the storeroom.&lt;/li&gt;
&lt;li&gt;The concierge takes a look over the store room and go "Yes, we've got some room at the designated &lt;code&gt;B4&lt;/code&gt; area and will allocate that space to store your luggages".&lt;/li&gt;
&lt;li&gt;They hand Timmy a &lt;strong&gt;pick-up card&lt;/strong&gt; with the designated area on it, &lt;code&gt;B4&lt;/code&gt; in our case.&lt;/li&gt;
&lt;li&gt;Timmy is happy, goes around doing whatever, and when he wants to pick-up his luggages, he goes back to the counter and show them his &lt;strong&gt;pick-up card&lt;/strong&gt;. "&lt;em&gt;Have you C-een my luggages?&lt;/em&gt;"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our example, Timmy's luggage is the &lt;strong&gt;data&lt;/strong&gt;, the &lt;strong&gt;pick-up card is the pointer&lt;/strong&gt; (it states where Timmy's bag is stored). The place that the concierge is storing Timmy's luggage is the &lt;strong&gt;memory block&lt;/strong&gt;, and the counter is the &lt;strong&gt;program&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By showing the counter (&lt;em&gt;the program&lt;/em&gt;) Timmy's card (&lt;em&gt;pointer/memory address&lt;/em&gt;), Timmy can retrieve his luggage (&lt;em&gt;data&lt;/em&gt;). Bonus? Because they know &lt;strong&gt;exactly&lt;/strong&gt; where Timmy's bag is stored -- &lt;code&gt;B4&lt;/code&gt;, this means that they can retrieve all Timmy's luggages relatively quickly!&lt;/p&gt;

&lt;p&gt;Also, ever wondered why you access elements in array with &lt;em&gt;index&lt;/em&gt;, like so?&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; first element&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; sixth element&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; hundredth element&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;This is because array holds the references to the memory block, and the index is telling it the &lt;em&gt;offset&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;An analogy of that is if I ask you to look for Timmy in a queue of 20 people, you would logically have to ask each of them if they were Timmy -- But, if I told you Timmy is the 6th (&lt;em&gt;index&lt;/em&gt;) from the first guy (&lt;em&gt;your original pointer&lt;/em&gt;), you know exactly where to look.&lt;/p&gt;

&lt;p&gt;Retrieving elements in arrays are fast exactly because of this - the program doesn't have to look through all 100 elements to find what you're looking for - if you've got the index, it just has to add the offset to the original memory address, and the droid that you were looking for will be right there!&lt;/p&gt;
&lt;h2&gt;
  
  
  What are dynamic arrays then?
&lt;/h2&gt;

&lt;p&gt;So I've told you a little about how array is represented in memory, but now it's time to talk about some cons.&lt;/p&gt;

&lt;p&gt;Remember how you have to explicitly declare the amount of memory you need? This means that the array will find a spot that will fit exactly your size; there's no guarantee that it will be able to fit more than what you have (because the memory block right behind it might be occupied).&lt;/p&gt;

&lt;p&gt;Back to our luggage analogy, think of it as if Timmy was to store 2 pieces of luggages, and &lt;code&gt;B4&lt;/code&gt;can store exactly 2 luggages, so they allocate that to Timmy. Now for some reason Timmy wants to store &lt;em&gt;another&lt;/em&gt; piece of luggage, but &lt;code&gt;B4&lt;/code&gt; can't store 3 luggages, only 2, so what do they do?&lt;/p&gt;

&lt;p&gt;They take all of his existing luggages, move it into a new spot that can fit more than 3 luggages, and then store all of them together.&lt;/p&gt;

&lt;p&gt;That is an expensive operation but it's exactly how memory works too!&lt;/p&gt;

&lt;p&gt;In Ruby, you don't have to declare a specific size &lt;em&gt;before hand&lt;/em&gt;, but that's because Ruby handles it for you automagically through dynamic array.&lt;/p&gt;

&lt;p&gt;What dynamic array does is that if the array is nearing its full capacity, it'll automatically declare a new, bigger array and move all existing elements into it, and the old array is then garbage collected. How much bigger? The growth factor is &lt;em&gt;usually&lt;/em&gt; 2; double the size of the current array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In fact, don't take my word for it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ruby has a &lt;a href="https://ruby-doc.org/core-2.2.0/ObjectSpace.html" rel="noopener noreferrer"&gt;ObjectSpace module&lt;/a&gt; that allows us to interact with current living object in memory. We can use this module to take a peek at the memory usage of our dynamic array,  sounds exactly like what we want!&lt;/p&gt;

&lt;p&gt;I have written a small Ruby script that calculates the growth factor of the dynamic array. Feel free to take a look at it &lt;a href="https://gist.github.com/edisonywh/c61b3ab50359e68454d87b2c13d5d1a8" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and if you do, you can see that Ruby arrays have a 1.5x growth factor (that is, they make an array that's 50% bigger on every copy)&lt;/p&gt;
&lt;h2&gt;
  
  
  I know what arrays are, what are linked lists?
&lt;/h2&gt;

&lt;p&gt;Keep in mind that although arrays and linked lists are both considered as linear data structures, they have one big difference amongst them.&lt;/p&gt;

&lt;p&gt;Elements in an array are stored literally right next to each other in memory (so we can have index for fast lookups), but nodes in linked lists have no such restriction (which is why there's no index lookup for linked lists) - each and every item can be stored anywhere on the memory block and it'd be fine.&lt;/p&gt;

&lt;p&gt;It's almost as if Timmy's trying to store 5 luggages, and the concierge does not have a space and decides to leave 5 of them all over the place. Sounds unorganized?&lt;/p&gt;

&lt;p&gt;Also if they are stored in different places, how do you know which bags are Timmy's? &lt;em&gt;Hint: Just keep track of the next node/bag!&lt;/em&gt; In our case, the concirege keeps them separately but with a tag on each of them that points to the next bag.&lt;/p&gt;

&lt;p&gt;A node in linked list consists of two parts - the data part and a pointer to the next node. This is how they're able to maintain the &lt;code&gt;linear&lt;/code&gt; part of it -- they still have the concept of order, they just don't have to be stored in order literally!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;node = [ data | pointer ]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, given the following example stored in memory:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[C | D] [A | B] [B | C] [D | nil]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This bits look like it's out of order - but if I had told you that the first element is &lt;code&gt;A&lt;/code&gt;, you would be able to tell me the exact order of the list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;list = [A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; nil]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There's a lot of interesting things that you can do with linked lists which I'm not going to dive into (also a lot on Big O that I didn't talk about), there are already a tonne of good articles out there on data structures. If you made it here, I suggest you to read from Ali's blogpost &lt;a href="https://dev.to/aspittel/thank-u-next-an-introduction-to-linked-lists-4pph"&gt;here&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/aspittel" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F38627%2F77a2a5e7-603e-41b4-afcc-f7aff468ae2f.jpg" alt="aspittel"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/aspittel/thank-u-next-an-introduction-to-linked-lists-4pph" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;thank u, next: an introduction to linked lists&lt;/h2&gt;
      &lt;h3&gt;Ali Spittel ・ Dec 5 '18&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#datastructures&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;You can also read more about List/Cons on &lt;a href="https://en.wikipedia.org/wiki/Cons" rel="noopener noreferrer"&gt;Wiki here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Note
&lt;/h2&gt;

&lt;p&gt;I initially wrote this article for a slightly different topic - &lt;a href="https://dev.to/edisonywh/-elixir--why-linked-lists--1e9d"&gt;[ Elixir | Why Linked List?]&lt;/a&gt;, but found it took way too long to explain how arrays work before I was able to explain explore why Elixir uses linked lists, hence I have separated them into two separate articles.&lt;/p&gt;

&lt;p&gt;In that article I talk about why functional langauges use linked lists as their linear data structure. Do check it out!&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/edisonywh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg" alt="edisonywh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/edisonywh/-elixir--why-linked-lists--1e9d" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;[ Elixir | Why Linked Lists? ]&lt;/h2&gt;
      &lt;h3&gt;Edison Yap ・ Feb 12 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#elixir&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#computerscience&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#functional&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;1) &lt;a href="https://medium.com/@rebo_dood/ruby-has-a-memory-problem-part-1-7887bbacc579" rel="noopener noreferrer"&gt;https://medium.com/@rebo_dood/ruby-has-a-memory-problem-part-1-7887bbacc579&lt;/a&gt; - This is where I found out about additional &lt;code&gt;ObjectSpace&lt;/code&gt; methods by requiring it&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>computerscience</category>
      <category>c</category>
    </item>
    <item>
      <title>Recursion, Tail Call Optimization and Recursion.</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Fri, 30 Nov 2018 09:27:31 +0000</pubDate>
      <link>https://dev.to/edisonywh/recursion-tail-call-optimization-and-recursion-2enh</link>
      <guid>https://dev.to/edisonywh/recursion-tail-call-optimization-and-recursion-2enh</guid>
      <description>&lt;p&gt;Recently I thought I'd take a jab at functional languages just to try out a different paradigm of thinking, and I decided to pick up &lt;code&gt;Elixir&lt;/code&gt; since it's so syntatically similar to &lt;code&gt;Ruby&lt;/code&gt;. While I was learning, I found something.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elixir has no loops&lt;/li&gt;
&lt;li&gt;Recursion makes an additional call to call stack (which is why your stack overflows if you forget your base case)&lt;/li&gt;
&lt;li&gt;Tail Call Optimizations is a compiler feature that optimizes recursions (if the last thing it does is call itself)!&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  There is no concept of loop in Elixir.
&lt;/h2&gt;

&lt;p&gt;Yes, none of your &lt;code&gt;for&lt;/code&gt; loops or &lt;code&gt;.each&lt;/code&gt;. It's a bit of a strange concept at first, but let's see what's the alternative.&lt;/p&gt;

&lt;p&gt;If you can't use an iterative approach to iterate a loop, how do you traverse a list? &lt;em&gt;Recursion&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Brief recap: Recursion is when a function calls itself, &lt;a href="https://dev.to/edisonywh/recursion-tail-call-optimization-and-recursion-2enh"&gt;like this&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A Ruby Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt; &lt;span class="c1"&gt;# shift takes out the first element&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Element: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty?&lt;/span&gt;
  &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&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;An Elixir Example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Recursion&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;([]),&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Done"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Element: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Pretty interesting to traverse recursively instead of the iteratively aye?&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  LARGE NUMBERS!
&lt;/h2&gt;

&lt;p&gt;Next, let's see what happens when it comes to a large number!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ruby&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;large_array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Array&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="mi"&gt;500_000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;large_array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 10918&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; stack level too deep (SystemStackError)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It crashes with a &lt;code&gt;SystemStackError&lt;/code&gt;! Wow, does that mean &lt;code&gt;recursion&lt;/code&gt; is bad? does that mean &lt;code&gt;recursion&lt;/code&gt; is bad? does the mean &lt;code&gt;recursion&lt;/code&gt; is bad? does that mean &lt;code&gt;recursion&lt;/code&gt; is ... Okay, before we go crazy, let's take a look at how Elixir deals with it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Elixir&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500000&lt;/span&gt;
&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iterate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;Recursion&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 500000&lt;/span&gt;
&lt;span class="c1"&gt;# Done&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; :ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can see that Elixir effortlessly handles all &lt;strong&gt;five hundred thousands&lt;/strong&gt; recursion. But why does it work in &lt;code&gt;Elixir&lt;/code&gt; but not &lt;code&gt;Ruby&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Because of  &lt;code&gt;Tail Call Optimization&lt;/code&gt;!&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Tail Call Optimization?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Tail Call Optimization (TCO)&lt;/strong&gt; is a compiler feature in which the compiler automatically optimizes the &lt;em&gt;stack&lt;/em&gt;, if the last thing that a function does is call itself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;But what is the stack?&lt;/em&gt; &lt;em&gt;and what do you mean by 'optimizes the stack'?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stack is a data structure that employs the LIFO mindset (Last-In-First-Out), think of it as a fixed array but you can only insert from one end and take out from the same end. What we're specifically talking about here is the call stack.&lt;/p&gt;

&lt;p&gt;To understand this better, we need to first understand how call stacks work.&lt;/p&gt;

&lt;p&gt;When you &lt;em&gt;call&lt;/em&gt; a function, it gets added to the call stack, and when it finish executing, it gets popped off the stack, like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r7l7o2zkirlds4q6m8d.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r7l7o2zkirlds4q6m8d.gif" alt="Call Stack Visualization" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when you have a recursive function call, it's basically adding itself to the call stack every single time. If you forget your base case/have too big of a call stack, it throws a stack overflow error! (what we saw earlier), like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2752v29el96in6aftnfj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2752v29el96in6aftnfj.gif" alt="Recursion Stack Visualization" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All TCO means is that, when the compiler sees the last call of the function is calling itself, instead of adding the function call to the call stack, it does a &lt;code&gt;goto&lt;/code&gt; instead, and basically restarts the same function call, requiring &lt;strong&gt;O(1)&lt;/strong&gt; instead of &lt;strong&gt;O(n)&lt;/strong&gt; space (we will prove this later).&lt;/p&gt;

&lt;p&gt;Now this is what it looks like when it's TCO-ed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zhe6ly487tecng1mvi8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zhe6ly487tecng1mvi8.gif" alt="TCO Call Stack Visualization" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Do we have that in Ruby?
&lt;/h2&gt;

&lt;p&gt;We do indeed! However TCO is not enabled by default, so you have to pass in the compile option during runtime, and you can do it like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;RubyVM&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;InstructionSequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile_option&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;tailcall_optimization: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;trace_instruction: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'./tail_recursion.rb'&lt;/span&gt;

&lt;span class="n"&gt;large_array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Array&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="mi"&gt;500_000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;large_array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Element: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Element: 500000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Not very useful if you have to enable it with a flag, but it's there if you need it!&lt;/p&gt;
&lt;h2&gt;
  
  
  Proof?
&lt;/h2&gt;

&lt;p&gt;Got your back! In RubyLand, We can inspect the current call stack with a call to &lt;code&gt;Kernel#caller&lt;/code&gt;. Lets &lt;code&gt;.count&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Change our code like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-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;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Element: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Stack Count: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; new line&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty?&lt;/span&gt;
  &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Normal ruby&lt;/span&gt;
&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;large_array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 1,           Stack Count: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...,         Stack Count: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...,         Stack Count: ...&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 10918,           Stack Count: 10918&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; stack level too deep (SystemStackError)&lt;/span&gt;

&lt;span class="c1"&gt;# TCO-enabled&lt;/span&gt;
&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;large_array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 1,           Stack Count: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...,         Stack Count: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: ...,         Stack Count: 1&lt;/span&gt;
&lt;span class="c1"&gt;# Element: 500000,          Stack Count: 1&lt;/span&gt;

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

&lt;/div&gt;

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

&lt;p&gt;Alright so that's it for today! Thought it's pretty cool that I'm trying to learn Elixir but end up learning something about compiler instead!&lt;/p&gt;

&lt;p&gt;Check out my previous articles too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/edisonywh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg" alt="edisonywh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/edisonywh/ruby-has-no-class-methods-39l5" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Ruby has no class methods&lt;/h2&gt;
      &lt;h3&gt;Edison Yap ・ Nov 9 '18&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#rails&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;div class="ltag__link"&gt;
&lt;br&gt;
  &lt;a href="/edisonywh" class="ltag__link__link"&gt;&lt;br&gt;
    &lt;div class="ltag__link__pic"&gt;
&lt;br&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87064%2F012a42e6-1f27-469d-8085-d19d29279d07.jpg" alt="edisonywh"&gt;&lt;br&gt;
    &lt;/div&gt;
&lt;br&gt;
  &lt;/a&gt;&lt;br&gt;
  &lt;a href="https://dev.to/edisonywh/my-ruby-journey-hooking-things-up--feg" class="ltag__link__link"&gt;&lt;br&gt;
    &lt;div class="ltag__link__content"&gt;
&lt;br&gt;
      &lt;h2&gt;My Ruby Journey: Hooking Things Up&lt;/h2&gt;
&lt;br&gt;
      &lt;h3&gt;Edison Yap ・ Nov 3 '18&lt;/h3&gt;
&lt;br&gt;
      &lt;div class="ltag__link__taglist"&gt;
&lt;br&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;&lt;br&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;&lt;br&gt;
        &lt;span class="ltag__link__tag"&gt;#rails&lt;/span&gt;&lt;br&gt;
      &lt;/div&gt;
&lt;br&gt;
    &lt;/div&gt;
&lt;br&gt;
  &lt;/a&gt;&lt;br&gt;
&lt;/div&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Attribution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.vecteezy.com" rel="noopener noreferrer"&gt;Russian doll cover image source&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>elixir</category>
      <category>computerscience</category>
      <category>functional</category>
    </item>
    <item>
      <title>Ruby has no class methods</title>
      <dc:creator>Edison Yap</dc:creator>
      <pubDate>Fri, 09 Nov 2018 11:19:02 +0000</pubDate>
      <link>https://dev.to/edisonywh/ruby-has-no-class-methods-39l5</link>
      <guid>https://dev.to/edisonywh/ruby-has-no-class-methods-39l5</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;Wait…. Whaaaaaaaaaaaaaaaaaaat?&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzs0vzzfds0vgl2kk8bl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzs0vzzfds0vgl2kk8bl.gif" alt="Mind Blown.gif" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I don’t know about you, but that was my exact reaction when I first heard &lt;code&gt;Nadia Odunayo&lt;/code&gt;’s talk in &lt;code&gt;RubyConf KL 2018&lt;/code&gt;  (mind you, one of the most captivating talk I’ve ever heard!), in which she showed that that &lt;em&gt;Ruby has no class methods.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When I first started learning Ruby, I’ve been told that Ruby basically have two types of methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class Methods&lt;/li&gt;
&lt;li&gt;Instance Methods
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-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;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_method&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This is a class method of &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&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;an_instance_method&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This is an instance method of &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&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="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_method&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; "This is a class method of Foo"&lt;/span&gt;
&lt;span class="no"&gt;Foo&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="nf"&gt;class_method&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; undefined method `class_method' for #&amp;lt;Foo:0x007f83960d2228&amp;gt; (NoMethodError)&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;an_instance_method&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; undefined method `an_instance_method' for Foo:Class (NoMethodError)&lt;/span&gt;
&lt;span class="no"&gt;Foo&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="nf"&gt;an_instance_method&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; This is an instance method of #&amp;lt;Foo:0x007f80aa92d150&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well at first, it makes a lot of sense —&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A class is an object, and when you call &lt;code&gt;.new&lt;/code&gt; on a class, you create a new instance of that object.&lt;/p&gt;

&lt;p&gt;Hence, instance methods are only available on instances of that class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay so then.. what am I talking about when I said Ruby has no class methods?&lt;/p&gt;

&lt;h2&gt;
  
  
  Parents. Grandparents.  Ancestors..!
&lt;/h2&gt;

&lt;p&gt;First thing we need to understand is something called &lt;strong&gt;Ancestor Chains&lt;/strong&gt; in Ruby.&lt;/p&gt;

&lt;p&gt;Ancestors Chains is vital to understanding how Ruby works; the entire method calling chain works because of the chain, but here I’ll attempt at an overly simplified explanation.&lt;/p&gt;

&lt;p&gt;In Ruby, everything inherits &lt;code&gt;Object&lt;/code&gt; , and every &lt;code&gt;Object&lt;/code&gt; inherits from a &lt;code&gt;BasicObject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s do a quick check!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mf"&gt;2.3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mo"&gt;001&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt;
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="mf"&gt;2.3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mo"&gt;002&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;superclass&lt;/span&gt; &lt;span class="c1"&gt;#superclass allow us to see what class the receiver inherited from. AKA, the parent of current class.&lt;/span&gt;
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;

&lt;span class="mf"&gt;2.3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mo"&gt;003&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;superclass&lt;/span&gt;
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;BasicObject&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Kernel&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="no"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;could&lt;/span&gt; &lt;span class="n"&gt;quickly&lt;/span&gt; &lt;span class="n"&gt;verify&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;doing&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you call a method, it calls the method on the current class; if the current class does not have that method, it goes up the ancestor chain until it finds one.&lt;/p&gt;

&lt;p&gt;Not clear yet? I’ve gotchu! &lt;em&gt;Cue weird metaphor&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine you’re just a kid, and that annoying Little Johnny who is trying to outsmart you asks you sheepishly: “Do you know what &lt;code&gt;.planets&lt;/code&gt; are in our solar system? I bet you don’t!”.&lt;/p&gt;

&lt;p&gt;Well you actually don’t know the answer, but you wouldn’t let Little Johnny get his victory, and so you make this your life goal to find out, and you ask your parents: “What are the &lt;code&gt;.planets&lt;/code&gt; in our solar system?”. And well, your parents don’t know that either, so &lt;em&gt;they&lt;/em&gt; go on and ask their parents, who happen to know the answer.&lt;/p&gt;

&lt;p&gt;Now it’s &lt;em&gt;your turn&lt;/em&gt; to smirk sheepishly back at Little Johnny and tell him what the answer is.&lt;/p&gt;

&lt;p&gt;So now whenever someone asks you what are the &lt;code&gt;.planets&lt;/code&gt; in our solar system, you have the answer, from your Grandparents.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How is this relevant? Well, we talked about how method lookup work, and in my &lt;a href="https://medium.com/fave-engineering/my-ruby-journey-hooking-things-up-91d757e1c59c" rel="noopener noreferrer"&gt;last post&lt;/a&gt; I talked about reusing methods (&lt;code&gt;Module&lt;/code&gt;) with &lt;code&gt;include&lt;/code&gt;,  &lt;code&gt;extend&lt;/code&gt; and &lt;code&gt;prepend&lt;/code&gt;. Now let’s look at their respective ancestor chains when you &lt;code&gt;include&lt;/code&gt;, &lt;code&gt;extend&lt;/code&gt; and &lt;code&gt;prepend&lt;/code&gt; a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Awesome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IncludeModule&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Awesome&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="no"&gt;IncludeModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [IncludeModule, Awesome, Object, Kernel, BasicObject]&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExtendModule&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Awesome&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="no"&gt;ExtendModule&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [ExtendModule, Object, Kernel, BasicObject]&lt;/span&gt;
&lt;span class="c1"&gt;# WHERE IS THE MODULE?!&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrependModule&lt;/span&gt;
  &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;Awesome&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="no"&gt;PrependModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [Awesome, PrependModule, Object, Kernel, BasicObject]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll realize something strange:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you &lt;code&gt;include&lt;/code&gt;, the module is being inserted &lt;em&gt;after&lt;/em&gt; the current class, which means it’ll be the first thing you hit when you can’t find a method on current class. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;prepend&lt;/code&gt; is the opposite of &lt;code&gt;include&lt;/code&gt;; it inserts &lt;em&gt;before&lt;/em&gt;, so technically, you still wouldn’t be able to call it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;You don’t see the module on the ancestor chain when you extend a module&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hold on, we know for a fact that when we &lt;code&gt;extend&lt;/code&gt; a module, we get access to &lt;em&gt;class methods&lt;/em&gt;, and we also know that method calling happen via ancestor chains, but.. how can &lt;code&gt;extend&lt;/code&gt; work if the module is not on the ancestor chain?&lt;/p&gt;

&lt;p&gt;The reason is because of something in Ruby called &lt;strong&gt;Singleton Class&lt;/strong&gt; (sometimes referred to as &lt;code&gt;Metaclass&lt;/code&gt;, &lt;code&gt;Anonymous Class&lt;/code&gt; or &lt;code&gt;Eigenclass&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Whenever you initialize a class in Ruby, two objects are generated — &lt;code&gt;Foo&lt;/code&gt; itself, and &lt;code&gt;Foo’s Singleton Class&lt;/code&gt;.  To prove this, in her talk Nadia showed a great way to inspect the current count of classes using &lt;code&gt;ObjectSpace&lt;/code&gt;. &lt;code&gt;ObjectSpace&lt;/code&gt; is basically a way for you to interact with all currently living Ruby objects in memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ObjectSpace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count_objects&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:T_CLASS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; 920&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="no"&gt;ObjectSpace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count_objects&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:T_CLASS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; 922&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, before we understand what &lt;code&gt;extend&lt;/code&gt; is doing,  we need to first understand &lt;code&gt;Singleton Class&lt;/code&gt;, so bear with me, get through the next section and we’ll then be able to understand the &lt;code&gt;extend&lt;/code&gt; magic!&lt;/p&gt;

&lt;h2&gt;
  
  
  The one about Singleton Class
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Singleton Class&lt;/code&gt;, like its alternative name, &lt;code&gt;Anonymous Class&lt;/code&gt; suggests, is anonymous, thus does not show up in the ancestor chain(!!!) That doesn’t mean we can’t access the singleton class though, in fact, there’s a handy method called &lt;code&gt;.singleton_class&lt;/code&gt; that we can use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Foo&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;singleton_class&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; #&amp;lt;Class:Foo&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s take a look at what is the &lt;code&gt;.class&lt;/code&gt; of the class you just created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Foo&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; Class&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may have noticed earlier, there's also an alternative way to initialize a class as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks really familiar now doesn’t it? Foo is an &lt;strong&gt;instance of Class&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why is this important? Because this means that the normal &lt;strong&gt;Class Methods&lt;/strong&gt; that you have come to think you’re executing on &lt;code&gt;Foo&lt;/code&gt; is in fact, an instance method on &lt;code&gt;Class&lt;/code&gt;. — It works because &lt;code&gt;Foo&lt;/code&gt; is an instance of &lt;code&gt;Class&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;We now know singleton classes aren’t visible on ancestor chains. Remember what else wasn’t visible on the chain? The module when you do an &lt;code&gt;extend&lt;/code&gt;! &lt;/p&gt;

&lt;p&gt;We can check the ancestor chain of the singleton class itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;HiddenModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;HiddenModule&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;singleton_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [#&amp;lt;Class:Foo&amp;gt;, HiddenModule, #&amp;lt;Class:Object&amp;gt;, #&amp;lt;Class:BasicObject&amp;gt;, Class, Module, Object, Kernel, BasicObject]&lt;/span&gt;

&lt;span class="c1"&gt;# Well HiddenModule is not so hidden anymore!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also prove that what we come to know as &lt;code&gt;class_method&lt;/code&gt;, is actually just an instance method on the singleton class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Mystery&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resolved?&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;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Mystery&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolved?&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;singleton_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instance_methods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/resolved?/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [:resolved?]&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:resolved?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; #&amp;lt;Method: Class(Mystery)#resolved?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now we know what &lt;code&gt;extend&lt;/code&gt;  actually does behind the scene is basically an &lt;code&gt;include&lt;/code&gt; on the singleton class itself. Let’s prove it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Bar&lt;/span&gt;
  &lt;span class="no"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;singleton_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Mystery&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolved?&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="no"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;singleton_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instance_methods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/resolved?/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [:resolved?]&lt;/span&gt;
&lt;span class="no"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:resolved?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; #&amp;lt;Method: Class(Mystery)#resolved?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Still works!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But yeah, writing the full form requires a staggering &lt;em&gt;three&lt;/em&gt; more words, that’s a lot of work, so we’ve got &lt;code&gt;extend&lt;/code&gt; to save us that keystroke instead!&lt;/p&gt;

&lt;h2&gt;
  
  
  Author’s Note
&lt;/h2&gt;

&lt;p&gt;I’ll be pumping out a few more articles, meanwhile do check-out my &lt;a href="https://medium.com/fave-engineering/my-ruby-journey-hooking-things-up-91d757e1c59c" rel="noopener noreferrer"&gt;last post&lt;/a&gt; on &lt;code&gt;included&lt;/code&gt;, &lt;code&gt;extended&lt;/code&gt; and &lt;code&gt;prepended&lt;/code&gt; hooks! &lt;/p&gt;

&lt;p&gt;Phew, that’s it for my second blogpost. Looking forward to learning and sharing more! &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
