<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2023-11-21T12:10:59+00:00</updated><id>/feed.xml</id><title type="html">Fran Martinez</title><subtitle>Hi, my name is Fran Martinez. I am a Ruby developer from Barcelona, Spain.</subtitle><entry><title type="html">Let me introduce myself</title><link href="/about/" rel="alternate" type="text/html" title="Let me introduce myself" /><published>2023-11-21T10:00:47+00:00</published><updated>2023-11-21T10:00:47+00:00</updated><id>/hi</id><content type="html" xml:base="/about/">&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://martinezcoder.github.io/&quot;&gt;My posts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/martinezcoder&quot;&gt;Github&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.linkedin.com/in/martinezcoder&quot;&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/users/1808649/fran-martinez&quot;&gt;Stackoverflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://exercism.org/profiles/martinezcoder&quot;&gt;Exercism&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hi&quot;&gt;&lt;strong&gt;Hi!&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;My name is &lt;strong&gt;Fran Martinez&lt;/strong&gt;. Nice to meet you! I am backend developer borned in Barcelona city, although since some years ago I live in an small village in the mountains of Almería, Spain. I started my career in 2005 but it was not until I discovered Ruby that I became a proud developer.&lt;/p&gt;

&lt;p&gt;I like all about development culture, from the product definition to development, but I what I most like is to share knowledge and open my mind to other points of view. Every product is the result of a shared artistic process; it’s the result of collaborative work, and that’s why I like responsible and enthusiastic people, who shares knowledge and who spend their time helping others to improve. This is at least what I try to do.&lt;/p&gt;

&lt;p&gt;I have been always looking to work remotely and it was on 2016 when I achieved this dream. Working remotely changed my life and now I am currently a proud evangelist of this working culture.&lt;/p&gt;

&lt;h2 id=&quot;what-can-i-do&quot;&gt;&lt;strong&gt;What can I do&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;-ruby-and-rails&quot;&gt;&amp;gt; Ruby (and Rails)&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;11 years&lt;/strong&gt; experience with Ruby. From the creation of a Rails app to a &lt;strong&gt;PORO&lt;/strong&gt; ruby coder. The Ruby community made me be reborn as a developer in 2012. Since then, I have learn the SOLID principles, design patterns, metaprogramming, git, TDD and Agile.&lt;/p&gt;

&lt;p&gt;About APIs, I have strong experience with GraphQL and JSONAPI. About databases, I can handle any SQL challenge and I have experience in NoSQL databases. Also, I briefly worked with Kafka some time ago for an event sourcing solution and I really loved it!&lt;/p&gt;

&lt;p&gt;I am clearly a backend developer, but I like to understand how frontend works and I am not afraid of having frontend (Javascript, React, Vue) challenges.&lt;/p&gt;

&lt;h3 id=&quot;-design-patterns-architecture&quot;&gt;&amp;gt; Design patterns, architecture&lt;/h3&gt;

&lt;p&gt;I like design patterns and architecture, take part on conversations about refactors, improvements and make scalable solutions. If a solution is not scalable and not easy to understand, then it is not a good solution.&lt;/p&gt;

&lt;h3 id=&quot;-javascript-and-frontend&quot;&gt;&amp;gt; Javascript and frontend&lt;/h3&gt;

&lt;p&gt;During my whole professional career I have been trying to keep myself updated in the frontend side. I am not afraid of Javascript, CSS and HTML and I like to play with frameworks like &lt;strong&gt;React&lt;/strong&gt; or &lt;strong&gt;Vue&lt;/strong&gt;. In the frontend area I feel like a junior that can easily become a senior in a very short time.&lt;/p&gt;

&lt;h3 id=&quot;-docker-and-kubernetes&quot;&gt;&amp;gt; Docker and Kubernetes&lt;/h3&gt;

&lt;p&gt;I used Docker on my local setup for a long time,  but it was not as fast as using native installations, so I decided to go back to native! However, I like the versatility of having services up and running without having to take care about installations. Is the best magic ever!&lt;/p&gt;

&lt;p&gt;About production environments, I have experience working with Kubernetes, updating memory size, number of replicas, environment variables, etc.&lt;/p&gt;

&lt;h3 id=&quot;-srumagile-and-kanban-workflows&quot;&gt;&amp;gt; Srum/Agile and Kanban workflows&lt;/h3&gt;

&lt;p&gt;I have worked with &lt;strong&gt;Scrum&lt;/strong&gt; and &lt;strong&gt;Kanban&lt;/strong&gt;. I have been &lt;strong&gt;scrum master and team lead&lt;/strong&gt; for a team of 7. On the discussion between &lt;strong&gt;Scrum&lt;/strong&gt; and &lt;strong&gt;Kanban&lt;/strong&gt; I reckon that I am in love with Kanban methodology when it has to be applied to asynchronous remote teams. However, I have years of experience working with Scrum and applying team workflows in companies.&lt;/p&gt;

&lt;h2 id=&quot;more-about-me&quot;&gt;&lt;strong&gt;More about me&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;I am a very friendly person who likes to learn every day as a way of life. I have many hoobies! That’s why I like remote jobs, in order to have this balance between professional and personal skills.&lt;/p&gt;

&lt;p&gt;I am a peaceful guy who likes to meet friends, practice yoga, play the guitar, learn about renewable energies, and of course reading about programming, philosophy, science fiction and classical novels. I also take care of my organic vegetable garden.&lt;/p&gt;</content><author><name></name></author><summary type="html">My posts Github LinkedIn Stackoverflow Exercism</summary></entry><entry><title type="html">Create a new Rails project with Docker compose</title><link href="/rails-project-with-docker/" rel="alternate" type="text/html" title="Create a new Rails project with Docker compose" /><published>2018-05-22T16:40:00+00:00</published><updated>2018-05-22T16:40:00+00:00</updated><id>/rails-from-docker</id><content type="html" xml:base="/rails-project-with-docker/">&lt;p&gt;This guide shows you how to use &lt;strong&gt;Docker Compose&lt;/strong&gt; to set up and run a &lt;strong&gt;Rails/PostgreSQL&lt;/strong&gt; app.&lt;/p&gt;

&lt;h2 id=&quot;define-the-project&quot;&gt;Define the project&lt;/h2&gt;

&lt;p&gt;We need to define four files to start a complete Rails project with Docker:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Dockerfile&lt;/li&gt;
  &lt;li&gt;docker-compose.yml&lt;/li&gt;
  &lt;li&gt;Gemfile&lt;/li&gt;
  &lt;li&gt;Gemfile.lock&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s start with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;. This file defines an image with a Docker container with Ruby and its dependencies. The application code will be located in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myapp&lt;/code&gt; directory.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Dockerfile&lt;/span&gt;

FROM ruby:2.5.0
RUN apt-get update &lt;span class=&quot;nt&quot;&gt;-qq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; build-essential libpq-dev nodejs
RUN &lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle &lt;span class=&quot;nb&quot;&gt;install
&lt;/span&gt;COPY &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; /myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will need an existent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gemfile.lock&lt;/code&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gemfile.lock&lt;/code&gt; is just an empty file:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;touch &lt;/span&gt;Gemfile.lock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;About the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt; we just need to create a bootstrap one to load Rails. It will be overwriteen with the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rails new&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Gemfile&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'https://rubygems.org'&lt;/span&gt;
gem &lt;span class=&quot;s1&quot;&gt;'rails'&lt;/span&gt;, &lt;span class=&quot;s1&quot;&gt;'5.1.5'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-comspose.yml&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# docker-compose.yml&lt;/span&gt;

version: &lt;span class=&quot;s1&quot;&gt;'3'&lt;/span&gt;
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;: bash &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;rm -f tmp/pids/server.pid &amp;amp;&amp;amp; bundle exec rails s -p 3000 -b '0.0.0.0'&quot;&lt;/span&gt;
    volumes:
      - .:/myapp
    ports:
      - &lt;span class=&quot;s2&quot;&gt;&quot;3000:3000&quot;&lt;/span&gt;
    depends_on:
      - db
    &lt;span class=&quot;nb&quot;&gt;tty&lt;/span&gt;: &lt;span class=&quot;nb&quot;&gt;true
    &lt;/span&gt;stdin_open: &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;See that we have defined a couple of services: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt;, and that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web&lt;/code&gt; depends on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt;. That means that the web knows the existence and have access to the database.&lt;/p&gt;

&lt;h2 id=&quot;build-the-project&quot;&gt;Build the project&lt;/h2&gt;

&lt;p&gt;Having those files created, you just need to run next command:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose run web rails new &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--force&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;postgresql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will see that the content of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt; has changed after this last command. Remember that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; moves this file into the image and we have now a different one. Because of this change, you need to build again:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;connect-the-database&quot;&gt;Connect the database&lt;/h2&gt;

&lt;p&gt;The service of the databse is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt; in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt;. We need to configure the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;database.yml&lt;/code&gt; in order to access to this service. To do so, we need to add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;host&lt;/code&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; part of the file:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;default: &amp;amp;default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;postgres-port&quot;&gt;Postgres port&lt;/h3&gt;

&lt;p&gt;By default, postgres opens the port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5432&lt;/code&gt;. If you have a running local instance of &lt;em&gt;postgres&lt;/em&gt; this port will be already opened and you won’t be able to run the Docker one. Given that case you have two options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;stop your local instance of postgres&lt;/li&gt;
  &lt;li&gt;change the port that uses your docker in you image. That could be done adding a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; tag in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;db&lt;/code&gt; configuration in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;try-it&quot;&gt;Try it!&lt;/h2&gt;

&lt;p&gt;Run migrations:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose run web bundle exec rails db:create db:migrate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will run all the services:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will run an specific service and its dependencies:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose run --service-ports web
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From here, you can work as always. Maybe it’s timne to create a new model. Remeber that you have to use docker in any call:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-compose run web bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rails g model User name:string
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">This guide shows you how to use Docker Compose to set up and run a Rails/PostgreSQL app.</summary></entry><entry><title type="html">Stub methods like Rspec</title><link href="/stub-methods-like-rspec/" rel="alternate" type="text/html" title="Stub methods like Rspec" /><published>2018-05-15T14:40:00+00:00</published><updated>2018-05-15T14:40:00+00:00</updated><id>/stub-methods-like-rspec</id><content type="html" xml:base="/stub-methods-like-rspec/">&lt;p&gt;Could you recognise what is it doing next code?&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;allow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:nickname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;and_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;martinezcoder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hi, this is martinezcoder!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sure! This is a call to stub a method with &lt;a href=&quot;https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance&quot;&gt;&lt;strong&gt;Rspec&lt;/strong&gt;&lt;/a&gt;. But,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Could you do the same without using Rspec?&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Do you know how to stub the method of a single instance?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, considering that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fran_user&lt;/code&gt; is an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt;, I am suposse to have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; class including the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hi&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nickname&lt;/code&gt;. When running the test, if the instance &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fran_user&lt;/code&gt; uses the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hi&lt;/code&gt;, it will respond &lt;em&gt;“Hi, this is martinezcoder!”&lt;/em&gt;, instead of the returned value defined in the method.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nickname&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;me&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;Hi, this is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nickname&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Last code will puts:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, this is me!&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;how-to-stub-the-instance-of-user-without-using-rspec&quot;&gt;How to stub the instance of User without using Rspec?&lt;/h3&gt;

&lt;p&gt;Stub the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nickname&lt;/code&gt; method of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fran_user&lt;/code&gt; instance:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nickname&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;martinezcoder&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fran_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Last code will puts:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, this is me!&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, this is martinezcoder!&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So you now know how to stub an instance method without the dependency of &lt;strong&gt;Rspec&lt;/strong&gt;!&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">Could you recognise what is it doing next code?</summary></entry><entry><title type="html">RESTful JSON API With Rails 5</title><link href="/restful-rails-json-api/" rel="alternate" type="text/html" title="RESTful JSON API With Rails 5" /><published>2018-05-15T08:40:00+00:00</published><updated>2018-05-15T08:40:00+00:00</updated><id>/restful-rails-api</id><content type="html" xml:base="/restful-rails-json-api/">&lt;p&gt;The &lt;a href=&quot;http://api.rubyonrails.org/&quot;&gt;Rails documentation&lt;/a&gt; says that &lt;em&gt;Ruby on Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern&lt;/em&gt;. However, several companies were using rails just to create a backend API, and that was translated into having tons of unnecessary code dependencies.  That’s why since version 5, Rails started offering a way to create an &lt;strong&gt;API-only&lt;/strong&gt; applications, which only provides the required things to have a super powerful API.&lt;/p&gt;

&lt;p&gt;Find a more detailed explanation in the &lt;a href=&quot;http://guides.rubyonrails.org/api_app.html&quot;&gt;API-only documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post is a self-guide to create a new API-only project. It’s not a tutorial that resolves and exercise, but a guide to remember the steps to follow in the creation of an API from scratch.&lt;/p&gt;

&lt;h2 id=&quot;creating-an-api-only-rails-project&quot;&gt;Creating an API-only rails project&lt;/h2&gt;

&lt;p&gt;To start creating an API-only Rails applications, ensure that your Rails version is equal or higher than 5.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; rails &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
Rails 5.2.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then create a new &lt;strong&gt;API-only&lt;/strong&gt; application:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; rails new my-api &lt;span class=&quot;nt&quot;&gt;--api&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--api&lt;/code&gt; part, tells Rails that we want an &lt;strong&gt;API-only&lt;/strong&gt; application. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-T&lt;/code&gt; part is necessary if you want to avoid the use &lt;em&gt;Minitest&lt;/em&gt; as the default testing framework. I normally use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rspec&lt;/code&gt;, so I would include it.&lt;/p&gt;

&lt;p&gt;Arriving here, you have a &lt;em&gt;Rails API-only&lt;/em&gt; application with these properties:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;It still provides things out of the box. It provices a set of middleware with all the stuff we could need for an API.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationController&lt;/code&gt; inherit from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActionController::API&lt;/code&gt; instead of from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActionController::Base&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;As any other Rails application, it will provide &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;routing&lt;/code&gt; resources and helpers, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caching&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;authentication&lt;/code&gt;, etc.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;project-setup&quot;&gt;Project Setup&lt;/h2&gt;

&lt;p&gt;This would depend on personal preferences, but I normally setup every new project with this gems:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/rspec/rspec-rails&quot;&gt;rspec-rails&lt;/a&gt; - I have nothing against &lt;em&gt;Minitest&lt;/em&gt;, but I normally use Rspec testing framework. It is needed to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rails generate rspec:install&lt;/code&gt; to install it in a project.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/thoughtbot/shoulda-matchers&quot;&gt;shoulda_matchers&lt;/a&gt; - Include more matchers to Rspec&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/thoughtbot/factory_bot_rails&quot;&gt;factory_bot_rails&lt;/a&gt; - Very userful tool to use fixtures.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/stympy/faker&quot;&gt;faker&lt;/a&gt; - A library for generating fake data such as names, addresses, and phone numbers.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/DatabaseCleaner/database_cleaner&quot;&gt;database_cleaner&lt;/a&gt; - We can ensure a clean environment before the execution of each test. It needs to include a piece of code in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rails_helper.rb&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Find &lt;a href=&quot;https://github.com/martinezcoder/coding-challenge-backend/pull/1/commits/6793b23de244f021b30880fff8a6bb8bb23df66d&quot;&gt;here&lt;/a&gt; a commit adding all these dependencies.&lt;/p&gt;

&lt;h2 id=&quot;api-endpoints&quot;&gt;API Endpoints&lt;/h2&gt;

&lt;p&gt;Now that we have our project to start, it’s the best moment o start thinking in the endpoints we will need. It will help us to have a clear idea of the models and the controllers that we will need.&lt;/p&gt;

&lt;p&gt;Write down a draft of the endpoints.&lt;/p&gt;

&lt;h2 id=&quot;model-definition&quot;&gt;Model definition&lt;/h2&gt;

&lt;p&gt;We have a list of endpoints, so we know more or less the main models and its associations.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create the database: models, associations, migrations&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TDD&lt;/strong&gt;: add tests to check associations and presence validations&lt;/li&gt;
  &lt;li&gt;Fix the tests adding the validations and associations to the models.&lt;/li&gt;
  &lt;li&gt;Create factories for each model using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Faker&lt;/code&gt; value. They will be useful in the next step.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point we have valid models and its associations and validations.&lt;/p&gt;

&lt;h2 id=&quot;controllers-and-routes&quot;&gt;Controllers and routes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Generate the required controllers with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rails g controller Name&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requests&lt;/code&gt; tests. You can use &lt;a href=&quot;https://github.com/martinezcoder/coding-challenge-backend/blob/test/spec/requests/zombies_spec.rb&quot;&gt;this one&lt;/a&gt; as a guide.&lt;/li&gt;
  &lt;li&gt;The errors in the tests will guide you to add the required routes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;responses&quot;&gt;Responses&lt;/h3&gt;

&lt;p&gt;I like controller responses with this structure:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;json: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;status: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Look at this &lt;a href=&quot;https://github.com/martinezcoder/coding-challenge-backend/blob/test/app/controllers/zombies_controller.rb&quot;&gt;example&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;exception-handler&quot;&gt;Exception handler&lt;/h3&gt;

&lt;p&gt;In the case that a searched record does not exist, Rails will through an error. Instead or raising the error, it would be necessary to rescue that error and notify the user with the problem.&lt;/p&gt;

&lt;p&gt;We should rescue any kind of error on which we need to notify the API client. To do this, we can create a concern:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# app/controllers/concerns/exception_handler.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ExceptionHandler&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# provides the more graceful `included` method&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;extend&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveSupport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Concern&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;included&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rescue_from&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;RecordNotFound&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;json: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;message: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;status: :not_found&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;rescue_from&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;RecordInvalid&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;json: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;message: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;status: :unprocessable_entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And include it in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationController&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# app/controllers/application_controller.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ApplicationController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActionController&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ExceptionHandler&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;versioning&quot;&gt;Versioning&lt;/h2&gt;

&lt;p&gt;It is not necessary to add &lt;em&gt;versioning&lt;/em&gt; in your first version. But when the day comes in which you have to add a second version, you can afford it in different ways.&lt;/p&gt;

&lt;p&gt;Have in mind that in any kind of solution you would have to&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Add a route constraint to select the version&lt;/li&gt;
  &lt;li&gt;Use different namespaces for the controllers, depending on the version.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are different options to apply versioning in our project. The most common are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Using &lt;a href=&quot;https://chriskottom.com/blog/2017/04/versioning-a-rails-api/&quot;&gt;different &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;routes&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;namespaces&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Selecting the version from the headers (using a &lt;a href=&quot;http://guides.rubyonrails.org/routing.html#advanced-constraints&quot;&gt;route constraint&lt;/a&gt;). Follow this &lt;a href=&quot;https://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-three#toc-versioning&quot;&gt;documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;serializers&quot;&gt;Serializers&lt;/h2&gt;

&lt;p&gt;I have always used &lt;a href=&quot;https://github.com/rails-api/active_model_serializers&quot;&gt;Active Models Serializers&lt;/a&gt;, but I recently discovered &lt;a href=&quot;https://github.com/Netflix/fast_jsonapi&quot;&gt;Fast JSON API&lt;/a&gt; from Netflix, and sounds promising. I will use that last one in my next project.&lt;/p&gt;

&lt;p&gt;https://github.com/Netflix/fast_jsonapi&lt;/p&gt;

&lt;h2 id=&quot;pagination&quot;&gt;Pagination&lt;/h2&gt;

&lt;p&gt;Any serious production API have to include pagination. &lt;a href=&quot;https://github.com/mislav/will_paginate&quot;&gt;Will_paginate&lt;/a&gt; or &lt;a href=&quot;https://github.com/kaminari/kaminari&quot;&gt;kaminari&lt;/a&gt; will do the trick.&lt;/p&gt;

&lt;h2 id=&quot;authentication-and-authorization&quot;&gt;Authentication and authorization&lt;/h2&gt;

&lt;p&gt;Follow &lt;a href=&quot;https://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-two&quot;&gt;this&lt;/a&gt; guide.&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;authorization&lt;/strong&gt;, the gem &lt;a href=&quot;https://github.com/varvet/pundit&quot;&gt;Pundit&lt;/a&gt; is great.&lt;/p&gt;

&lt;h2 id=&quot;caching&quot;&gt;Caching&lt;/h2&gt;

&lt;p&gt;Conditional &lt;strong&gt;GET&lt;/strong&gt;s: Rails handles conditional GET (&lt;strong&gt;ETag&lt;/strong&gt; and &lt;strong&gt;Last-Modified&lt;/strong&gt;) processing request headers and returning the correct response headers and status code. All you need to do is use the &lt;a href=&quot;http://api.rubyonrails.org/v5.2.0/classes/ActionController/ConditionalGet.html#method-i-stale-3F&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stale?&lt;/code&gt;&lt;/a&gt; check in your controller, and Rails will handle all of the HTTP details for you.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">The Rails documentation says that Ruby on Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. However, several companies were using rails just to create a backend API, and that was translated into having tons of unnecessary code dependencies. That’s why since version 5, Rails started offering a way to create an API-only applications, which only provides the required things to have a super powerful API.</summary></entry><entry><title type="html">Strategy pattern and singleton behavior</title><link href="/strategy-pattern/" rel="alternate" type="text/html" title="Strategy pattern and singleton behavior" /><published>2018-05-13T16:00:00+00:00</published><updated>2018-05-13T16:00:00+00:00</updated><id>/strategy-pattern</id><content type="html" xml:base="/strategy-pattern/">&lt;p&gt;Imagine that you have a collection of objects representing &lt;em&gt;animals&lt;/em&gt;. Some of them can fly and some others not. You would like to include the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fly&lt;/code&gt; in all them, although not all them can fly, so you go with the &lt;strong&gt;Template Method Design Pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;NotImplementedError&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Lion&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;I can't fly!&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;I am flying!&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Depending on the class, you decide whether it can fly or not. But this is not very clever, because we will be creating a lot of duplicate code. We will have as many &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fly&lt;/code&gt; methods as animals we create, and all of them will have the content repeated again and again.&lt;/p&gt;

&lt;p&gt;We can improve the architecture of the code with &lt;strong&gt;Strategy Design Pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;strategy-design-pattern&quot;&gt;Strategy Design Pattern&lt;/h2&gt;

&lt;p&gt;We have two options (&lt;strong&gt;strategies&lt;/strong&gt;) to decide to include on each animal: &lt;em&gt;it can fly&lt;/em&gt; or &lt;em&gt;it can not&lt;/em&gt;. So we create a module containing both strategies:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Flys&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ItFlys&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I am flying&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CantFly&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We decide that animals can’t fly as a default. And we include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CanFly&lt;/code&gt; strategy in the animals that can fly:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Flys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CantFly&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Lion&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Flys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ItFlys&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; lion &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Lion.new
irb&amp;gt; lion.fly
&lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
irb&amp;gt; duck &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Duck.new
irb&amp;gt; duck.fly
&lt;span class=&quot;s2&quot;&gt;&quot;I am flying&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;asigning-it-by-injection&quot;&gt;Asigning it by injection&lt;/h2&gt;

&lt;p&gt;We can still go further and apply the behavior on each instance by injection:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Flys&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ItFlys&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I am flying&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CantFly&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Animal&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:fly_behavior&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fly_behavior&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Flys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CantFly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fly_behavior&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fly_behavior&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fly_behavior&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Lion&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Animal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; lion &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Lion.new
irb&amp;gt; lion.fly
&lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
irb&amp;gt; duck &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Duck.new&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Flys::CantFly&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
irb&amp;gt; duck.fly
&lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
irb&amp;gt; duck.fly_behavior &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Flys::ItFlys
irb&amp;gt; duck.fly
&lt;span class=&quot;s2&quot;&gt;&quot;I am flying&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For the issue of flying animals, this last way of doing strategy design seems not the most appropiate. All ducks fly, so I personally prefer the previous solution for this case.&lt;/p&gt;

&lt;p&gt;But, we have seen that in this last example we have the option to change the &lt;em&gt;fly_behavior&lt;/em&gt; of an instance. How could we do this with the code of the previous solution? I will do it in the next example using a &lt;strong&gt;mixin&lt;/strong&gt; to change the flying behavior of an instance. That means, overwrite a &lt;strong&gt;singleton method&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;singleton-behavior&quot;&gt;Singleton behavior&lt;/h2&gt;

&lt;p&gt;But wait, a duck with 1 day of life is not able to fly, so, can a duck fly? Well, it depends. Next code will demostrate how to change this behaviour depending on the value of the instance variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the duck is old enough, it can fly!&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Flys&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ItFlys&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I am flying&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CantFly&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fly&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I can't fly&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Duck&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Flys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CantFly&lt;/span&gt;

  &lt;span class=&quot;no&quot;&gt;DAYS_TO_FLY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@can_fly&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;try_to_fly&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;days&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;days&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;try_to_fly&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;kp&quot;&gt;private&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;try_to_fly&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@can_fly&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Flys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ItFlys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DAYS_TO_FLY&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this solution, an instance of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt; will be able to fly at its seventh week of life, but not before.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">Imagine that you have a collection of objects representing animals. Some of them can fly and some others not. You would like to include the method fly in all them, although not all them can fly, so you go with the Template Method Design Pattern.</summary></entry><entry><title type="html">Fibers, Procs and Enumerables</title><link href="/enumerables-and-fibers/" rel="alternate" type="text/html" title="Fibers, Procs and Enumerables" /><published>2018-05-13T16:00:00+00:00</published><updated>2018-05-13T16:00:00+00:00</updated><id>/enumerables-and-fibers</id><content type="html" xml:base="/enumerables-and-fibers/">&lt;p&gt;Last day I spend some hours investigating a bit deeply about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt;. It is that kind of stuff that shows the magic of the &lt;strong&gt;Ruby&lt;/strong&gt; language. Once you have the whole control of enumerables, you feel the power of being a Ruby developer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fibers&lt;/strong&gt;, &lt;strong&gt;Procs&lt;/strong&gt; and &lt;strong&gt;Enumerables&lt;/strong&gt; are different things in &lt;strong&gt;Ruby&lt;/strong&gt;. But I will resolve the very traditional example of showing prime numbers with each one of them.&lt;/p&gt;

&lt;p&gt;We will define a module with the common code to find the prime numbers:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;PrimeFinder&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;private&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_next_prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prime?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prime?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;proc&quot;&gt;Proc&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; saves the last state of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; variable, so in every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; it will return the value of the next prime number:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Prime&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PrimeFinder&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;proc&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_next_prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Prime.new.proc
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Proc:test/fiber.rb&amp;gt;&lt;/span&gt;
irb&amp;gt; p.call
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 2
irb&amp;gt; p.call
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
irb&amp;gt; p.call
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 5
irb&amp;gt; p.call
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 7
irb&amp;gt; 5.times &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; puts p.call &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
11
13
17
19
23
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; objects picks up the surrounding environment. Any variables that are visible when a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; is created remain visible inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; when it is run. That’s why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; object is the same as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; outside.&lt;/p&gt;

&lt;p&gt;Also, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; always returns the last value computed in the code block. This is because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; objects have a lot in common with methods, so the last expression in the block will be the returned value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In summary, this solution does the trick, but it doesn’t seem the best solution for this issue.&lt;/p&gt;

&lt;h2 id=&quot;fiber&quot;&gt;Fiber&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://ruby-doc.org/core-2.4.1/Fiber.html&quot;&gt;Fibers&lt;/a&gt; are light weight primitives in the Ruby standard library which can be paused, resumed and scheduled manually. Fibers are commonly used in combination of concurrence processes, because they simplify asynchronous code.&lt;/p&gt;

&lt;p&gt;In this case, it will just return &lt;em&gt;next&lt;/em&gt; prime number and &lt;em&gt;pause&lt;/em&gt; the loop.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Prime&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PrimeFinder&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fiber&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Fiber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;no&quot;&gt;Fiber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_next_prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; f &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Prime.new.fiber
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Fiber:(irb):3 (created)&amp;gt;&lt;/span&gt;
irb&amp;gt; f2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  Prime.new.fiber
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Fiber:(irb):3 (created)&amp;gt;&lt;/span&gt;
irb&amp;gt; f.resume
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 2
irb&amp;gt; f.resume
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
irb&amp;gt; 3.times &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; puts f.resume &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
5
7
11
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
irb&amp;gt; 3.times &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; puts f2.resume &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
2
3
5
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fiber#yield&lt;/code&gt; returns control back to the context that resumed the Fiber and returns the value which was passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fiber#resume&lt;/code&gt;. Each defined fiber, will control its own independent state. That’s why it is useful in concurrence executions.&lt;/p&gt;

&lt;h2 id=&quot;enumerable&quot;&gt;Enumerable&lt;/h2&gt;

&lt;h3 id=&quot;option-a&quot;&gt;Option A&lt;/h3&gt;

&lt;p&gt;The traditional way with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;include Enumerable&lt;/code&gt; and defining an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; method. This way, we can use all the methods for enumerables like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt;, etc.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Prime&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PrimeFinder&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Enumerable&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_next_prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; of &lt;strong&gt;Enumerable&lt;/strong&gt; always start by the first prime number &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;. But if we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_enum&lt;/code&gt;, the result saves the last state of the loop.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Prime.new
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Prime:&amp;gt;&lt;/span&gt;
irb&amp;gt; p.take&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2, 3, 5]
irb&amp;gt; p.first&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2, 3, 5]
irb&amp;gt; e &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; p.to_enum
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Enumerator: #&amp;lt;Prime:&amp;gt;:each&amp;gt;&lt;/span&gt;
irb&amp;gt; e.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 2
irb&amp;gt; e.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
irb&amp;gt; e.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 5
irb&amp;gt; e.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 7
irb&amp;gt; e.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;option-b&quot;&gt;Option B&lt;/h3&gt;

&lt;p&gt;Instead of using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;include Enumerable&lt;/code&gt;, we define an enumerable for an specific method. Is the same as using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_enum&lt;/code&gt; in the last example.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Prime&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PrimeFinder&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enum&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enum_for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:enum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;block_given?&lt;/span&gt;

    &lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_next_prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb&amp;gt; p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; Prime.new.enum
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;Enumerator: #&amp;lt;Prime:&amp;gt;:enum&amp;gt;&lt;/span&gt;
irb&amp;gt; p.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 2
irb&amp;gt; p.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 3
irb&amp;gt; p.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 5
irb&amp;gt; p.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 7
irb&amp;gt; p.take&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2, 3, 5]
irb&amp;gt; p.first&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2, 3, 5]
irb&amp;gt; p.next
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note how the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p.next&lt;/code&gt; has continue the list showed before in the last call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p.next&lt;/code&gt;. But in the middle, we called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p.take&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p.first&lt;/code&gt; and the index of the state was not altered.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Ruby&lt;/strong&gt; offers us different ways to do the same. In this case, the use of &lt;strong&gt;Enumerable&lt;/strong&gt; is the best approach.&lt;/p&gt;

&lt;p&gt;I have been playing with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fiber&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; to achieve similar results, but their purpose are not to resolve this kind of issues. Also, I investigate about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fiber&lt;/code&gt; and I found that &lt;strong&gt;Matz&lt;/strong&gt; &lt;a href=&quot;https://bugs.ruby-lang.org/issues/8572&quot;&gt;proposed&lt;/a&gt; to include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fiber&lt;/code&gt; by default. The proposal was finally rejected because they didn’t find any reasonable use case.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">Last day I spend some hours investigating a bit deeply about Enumerable. It is that kind of stuff that shows the magic of the Ruby language. Once you have the whole control of enumerables, you feel the power of being a Ruby developer.</summary></entry><entry><title type="html">FIFO queues!</title><link href="/fifo-queues/" rel="alternate" type="text/html" title="FIFO queues!" /><published>2018-05-10T15:14:47+00:00</published><updated>2018-05-10T15:14:47+00:00</updated><id>/fifo-queues</id><content type="html" xml:base="/fifo-queues/">&lt;p&gt;When I was a C developer we used very frequently &lt;strong&gt;FIFO queues&lt;/strong&gt;. &lt;strong&gt;FIFO&lt;/strong&gt; means &lt;em&gt;first input, first output&lt;/em&gt;. This kind of queues have a fixed size. When the queue is full of elements, if a new element is enqueued, the first element enqueued has to fall out (has to be returned and removed from the queue).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How would you implement this behavior with Ruby language?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is how I would do it:&lt;/p&gt;

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

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FIFOQueue&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:arr&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@arr&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shift&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_fifo&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;FIFOQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Adding the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_fifo&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; class permits us to create a new Array as we normally do and convert it a &lt;strong&gt;FIFO queue&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; irb &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; fifo_queue.rb
irb&amp;gt; b &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;, 2, 3].to_fifo
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;#&amp;lt;FIFOQueue: @size=3, @arr=[&quot;a&quot;, 2, 3]&amp;gt;&lt;/span&gt;
irb&amp;gt; b.push &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;
irb&amp;gt; b.arr
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2, 3, &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">When I was a C developer we used very frequently FIFO queues. FIFO means first input, first output. This kind of queues have a fixed size. When the queue is full of elements, if a new element is enqueued, the first element enqueued has to fall out (has to be returned and removed from the queue).</summary></entry></feed>