<?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: Aweys Ahmed</title>
    <description>The latest articles on DEV Community by Aweys Ahmed (@aweysahmed).</description>
    <link>https://dev.to/aweysahmed</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%2F175233%2F8d8dcabc-a557-493a-9661-1d7f63ce269b.jpg</url>
      <title>DEV Community: Aweys Ahmed</title>
      <link>https://dev.to/aweysahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aweysahmed"/>
    <language>en</language>
    <item>
      <title>First time guest on a Podcast.</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Fri, 20 Aug 2021 15:57:16 +0000</pubDate>
      <link>https://dev.to/aweysahmed/first-time-guest-on-a-podcast-hog</link>
      <guid>https://dev.to/aweysahmed/first-time-guest-on-a-podcast-hog</guid>
      <description>&lt;p&gt;Hey everyone, &lt;/p&gt;

&lt;p&gt;I'm exited to start blogging again but in the meantime, I wanted to share a link to a podcast that I was a guest on recently. &lt;br&gt;
&lt;a href="https://www.codewithjason.com/code-with-jason-podcast/episodes/103-from-junior-to-intermediate-with-aweys-ahmed-q0qcp5wc/"&gt;https://www.codewithjason.com/code-with-jason-podcast/episodes/103-from-junior-to-intermediate-with-aweys-ahmed-q0qcp5wc/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was an honour to be a Guest on Jason Swett's podcast. In the episode we discussed how new developers can level up and some of the obstacles that new developers face. &lt;/p&gt;

&lt;p&gt;I hope you enjoy the podcast and I would checkout Jason's other podcasts and material as well. &lt;a href="https://www.codewithjason.com"&gt;https://www.codewithjason.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>podcast</category>
      <category>rails</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What are Service Objects in Ruby on Rails? Should you use it?</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Mon, 16 Mar 2020 12:55:06 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-are-service-objects-in-ruby-on-rails-should-you-use-it-20o2</link>
      <guid>https://dev.to/aweysahmed/what-are-service-objects-in-ruby-on-rails-should-you-use-it-20o2</guid>
      <description>&lt;p&gt;In a previous blog post, I mentioned that it was considered good practice to keep your controllers skinny and add the bloat to your model. However, this is not always desirable. &lt;/p&gt;

&lt;p&gt;Using service objects we can try and keep both our controller and model lean, readable and easier to test. This will make everyone happy. &lt;/p&gt;

&lt;h1&gt;
  
  
  What are Service Objects?
&lt;/h1&gt;

&lt;p&gt;Service Objects are plain old Ruby Objects (PORO). They are created to remove the bloat from your controller and/or model. &lt;/p&gt;

&lt;p&gt;It is intended to move business logic into separate files that you can be used elsewhere. &lt;/p&gt;

&lt;p&gt;Service objects should adhere to the single responsibility principle. This means that your service object should focus on doing one thing.&lt;/p&gt;

&lt;p&gt;It is important that your service object method returns something substantial and meaningful like an object.&lt;/p&gt;

&lt;p&gt;Let's look at an example of code that needs to be refactored. In the example below, we'll have an Employee's controller that renders the employees details but what we'll be focusing on is the calculation of federal and provincial taxes (This is not based on actual tax rates in Canada).&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;EmployeesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="ss"&gt;:set_employee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:destroy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="c1"&gt;# GET /employees/1&lt;/span&gt;
  &lt;span class="c1"&gt;# GET /employees/1.json&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
    &lt;span class="vi"&gt;@federal_tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="vi"&gt;@provincial_tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;20&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;So you can see that we are calculating the tax rates and storing them in an instance variable to be rendered in the show view. This works but isn't optimal. &lt;/p&gt;

&lt;p&gt;Let's see how service objects can help us.&lt;/p&gt;

&lt;h1&gt;
  
  
  How do we use a Service Object?
&lt;/h1&gt;

&lt;p&gt;The first thing we need to is to create a services folder under the app directory. Rails will autoload anything under the app directory. &lt;/p&gt;

&lt;p&gt;Then we will create a class called ApplicationService that our service objects will inherit from.&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;ApplicationService&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;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This above class method creates a new instance of the class with the arguments that are passed into it and calls the call method on the instance. &lt;/p&gt;

&lt;p&gt;(WHATTTTTT?) &lt;/p&gt;

&lt;p&gt;Let's look at an example to make sense of what that means. &lt;/p&gt;

&lt;p&gt;Let's create a service object that converts kilometres into meters.&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;KilometresToMeters&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:measurement&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;measurement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="vi"&gt;@measurement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;measurement&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;call&lt;/span&gt; 
  &lt;span class="n"&gt;measurement&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we didn't inherit from the ApplicationService, this is what our service object would look like in our controller.&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;calculate&lt;/span&gt;
 &lt;span class="vi"&gt;@distance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;KilometresToMeters&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="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:value&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the same call using the ApplicationService inheritance. Since we created an instance of the class in our ApplicationService, we don't need to do it when we use the service object in the controller.&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;calculate&lt;/span&gt;
 &lt;span class="vi"&gt;@distance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;KilometresToMeters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:value&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;It isn't the biggest change but I prefer this way of shortening the service object call and it increases the readability of the code.&lt;/p&gt;

&lt;p&gt;Now let's implement our service objects for our tax calculator.&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;FederalTaxAmount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationService&lt;/span&gt;
 &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:salary&lt;/span&gt;

 &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="vi"&gt;@salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;salary&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;call&lt;/span&gt;
  &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&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;Let's repeat the process for the provincial tax calculation.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProvincialTaxAmount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationService&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:salary&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;salary&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;call&lt;/span&gt;
    &lt;span class="vi"&gt;@salary&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;20&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;Now that we have created our service objects, let's see what they will look like in the controller.&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;show&lt;/span&gt;
    &lt;span class="vi"&gt;@federal_tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;FederalTaxAmount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@provincial_tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ProvincialTaxAmount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;salary&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;This controller looks cleaner than what we used in the beginning. This was a simple example for illustration purposes but you can imagine how this could clean up a larger and more complex controller.&lt;/p&gt;

&lt;p&gt;The service objects have also made it easier to determine what our application is doing. &lt;/p&gt;

&lt;p&gt;Here is a view of what the services folder looks like. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgfq5fy45h1jf3wg3qmzx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgfq5fy45h1jf3wg3qmzx.png" alt="Alt Text" width="347" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the services folder of this simple application gives you a very good idea of what this application does. This can also be very helpful for larger projects. &lt;/p&gt;

&lt;p&gt;I hope you found this blog helpful and that you can implement a service object in your next project to keep you controllers and models lean. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to create a basic search form and how to use Elasticsearch as an alternative.(Ruby on Rails)</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Thu, 05 Mar 2020 15:22:23 +0000</pubDate>
      <link>https://dev.to/aweysahmed/how-to-create-a-basic-search-form-and-how-to-use-elasticsearch-as-an-alternative-3o99</link>
      <guid>https://dev.to/aweysahmed/how-to-create-a-basic-search-form-and-how-to-use-elasticsearch-as-an-alternative-3o99</guid>
      <description>&lt;p&gt;In this the tutorial, we will review how to build a search form and then we'll look at how to implement a search form using ElasticSearch. I'll be using a book review app that I built recently. &lt;/p&gt;

&lt;p&gt;I want to implement a search function that will allow users to search the database for a book that has been reviewed. &lt;/p&gt;

&lt;h1&gt;
  
  
  Basic Rails Search
&lt;/h1&gt;

&lt;p&gt;In the app, a user can have more than one book review and a book review belongs to a user. &lt;/p&gt;

&lt;p&gt;Let's look at what the respective models look like. &lt;/p&gt;

&lt;p&gt;This is the book review model.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookReview&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the user model.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:book_reviews&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll have to work with the model, view and controller in this app. &lt;/p&gt;

&lt;p&gt;The first thing we'll do is add the search function to our view. We'll be using the index view to add the search function. &lt;/p&gt;

&lt;h2&gt;
  
  
  Modifying the view
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= form_with url: book_reviews_path, method: :get, local: true  do |form| %&amp;gt;
    &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;label&lt;/span&gt; &lt;span class="ss"&gt;:search&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Search Books:"&lt;/span&gt;&lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= form.text_field :search, id: :search, class: "form-control" %&amp;gt;
    &amp;lt;br /&amp;gt;
    &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt; &lt;span class="s2"&gt;"Submit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;class: &lt;/span&gt;&lt;span class="s1"&gt;'btn btn-primary'&lt;/span&gt;&lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;%&lt;/span&gt;&lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="sx"&gt;%&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create our search form and our controller is waiting to receive the params[:search] and that is set in the form.text_field. &lt;/p&gt;

&lt;h2&gt;
  
  
  Modifying the Book Review Model
&lt;/h2&gt;

&lt;p&gt;The next thing we want to do is add the logic for our search to the model. The reason we are adding it to our model and not our controller is that it is good practice to keep your controllers lean and add logic to your models.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;

&lt;span class="c1"&gt;# BookReview Model with validations&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookReview&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&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;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;
      &lt;span class="n"&gt;book_reviews&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BookReview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'title LIKE ?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;book_reviews&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt;
        &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="n"&gt;book_reviews&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;all&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;all&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;(I'm using Rails 5.1.7 and with this comes the ability to use all instead of typing BookReview.all)&lt;/p&gt;

&lt;p&gt;Our search function will render on the index page. If there isn't a search parameter entered it will render all the books. It will also render all the books if the search doesn't find a match. &lt;/p&gt;

&lt;p&gt;Now that we have seen how a basic search form is added to a rails project, let's look at what we can use that will allow us to scale and have a more efficient and robust search. &lt;/p&gt;

&lt;p&gt;The current search we are using works well for a small project but large scale applications will need something else. &lt;/p&gt;

&lt;h2&gt;
  
  
  ElasticSearch
&lt;/h2&gt;

&lt;p&gt;Elasticsearch is an open-source search and analytics engine. It uses a REST API that makes it easy to use. &lt;/p&gt;

&lt;p&gt;It has multiple uses but for this blog and tutorial, were are going to use it as a search engine. &lt;/p&gt;

&lt;p&gt;If you are using brew you can issue this command to install elasticsearch&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install elasticsearch&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;To use elasticsearch in our rails app, we'll need to add this gem and run bundle install.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gem 'searchkick"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Then we need to add searchkick to the model that we would like to search. In our example, we would add it to your book_reviews model.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookReview&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;searchkick&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you'll want to add data to the search index.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails searchkick:reindex CLASS=BookReview"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Type elasticsearch in the command line and then you can type&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl http://localhost:9200&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;in a separate window to make sure elasticsearch is up and running.&lt;/p&gt;

&lt;p&gt;We don't need to make any changes to our index view, however, we will need to make changes to our model and controller. &lt;/p&gt;

&lt;p&gt;Let's add the search logic to our model so that we are adhering to the design principle of fat models and skinny controllers.&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="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;advanced_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_results&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;search_results&lt;/span&gt;
      &lt;span class="no"&gt;BookReview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_results&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;fields: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;all&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's add this method to our controller&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;index&lt;/span&gt;
    &lt;span class="vi"&gt;@book_reviews&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BookReview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;advanced_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:search&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;This is all we need to do to add elasticsearch. ElasticSearch will yield results out-of-the-box even if the user hasn't spelled the word correctly.&lt;/p&gt;

&lt;p&gt;ElasticSearch can do some much more but I hope this tutorial has given you the knowledge on how to add it to your rails application.  &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is Model-View-Controller (MVC)? (Ruby on Rails perspective)</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Thu, 27 Feb 2020 13:42:42 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-model-view-controller-mvc-ruby-on-rails-perspective-3d85</link>
      <guid>https://dev.to/aweysahmed/what-is-model-view-controller-mvc-ruby-on-rails-perspective-3d85</guid>
      <description>&lt;p&gt;Model-View-Controller (MVC) is a software design pattern invented in 1978 by Trygve Reenskaug while he was a visiting scientist at the Learning Research Group (LRG) at Xerox Parc. &lt;/p&gt;

&lt;p&gt;The goal of MVC was to resolve the issue of users controlling a large and complex data set. &lt;/p&gt;

&lt;p&gt;MVC was designed for desktop use but has been adapted for web applications and can be used differently depending on the framework. &lt;/p&gt;

&lt;p&gt;Let's look into the three parts of MVC and how they work together to create a Ruby on Rails application. &lt;/p&gt;

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;In rails, the Control is called Action Controller. (Throughout the blog I will call it Controller instead of Action Controller)&lt;/p&gt;

&lt;p&gt;The controller works with the view and the model in a rails application by acting as an intermediary. When an HTTP request is made the controller will determine the correct controller class and method to use. &lt;/p&gt;

&lt;p&gt;Let's generate a scaffold to demonstrate how the controller works in a simple application.&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="c1"&gt;# frozen_string_literal: true&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BooksController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="ss"&gt;:set_book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:destroy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="c1"&gt;# GET /books&lt;/span&gt;
  &lt;span class="c1"&gt;# GET /books.json&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="vi"&gt;@books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&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 have set the root of the application to the books#index route. So when the user reaches the root path of our application, the book's controller will know to use the index method. &lt;/p&gt;

&lt;p&gt;Our index method uses Active Record to pull all the books from our database and stores it in an instance variable. Then we can use our index view to render all books.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p id="notice"&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;h1&amp;gt;Books&amp;lt;/h1&amp;gt;
&amp;lt;table&amp;gt;
  &amp;lt;thead&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;th&amp;gt;Title&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Author's First name&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Author's Last name&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Year published&amp;lt;/th&amp;gt;
      &amp;lt;th colspan="3"&amp;gt;Settings&amp;lt;/th&amp;gt;
    &amp;lt;/tr&amp;gt;
  &amp;lt;/thead&amp;gt;
  &amp;lt;tbody&amp;gt;
    &amp;lt;% @books.each do |book| %&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= book.title %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= book.author_first_name %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= book.author_last_name %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= book.year_published %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', book %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_book_path(book) %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
    &amp;lt;% end %&amp;gt;
  &amp;lt;/tbody&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;%= link_to 'New Book', new_book_path %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  View
&lt;/h2&gt;

&lt;p&gt;The job of the view is to present information to the user. To display content from the model that was retrieved using the controller, we need to use a template engine. In the above example, we used embedded Ruby.&lt;/p&gt;

&lt;p&gt;We can also use views to send data to our controller. The controller will then use Active Record which works with our model to save data to the database.&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="c1"&gt;# POST /books&lt;/span&gt;
  &lt;span class="c1"&gt;# POST /books.json&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="vi"&gt;@book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Book&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="n"&gt;book_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;respond_to&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
        &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s1"&gt;'Book was successfully created.'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :created&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;location: &lt;/span&gt;&lt;span class="vi"&gt;@book&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :unprocessable_entity&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;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="c1"&gt;# Only allow a list of trusted parameters through.&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;book_params&lt;/span&gt;
    &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:book&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:author_first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:author_last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:year_published&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;The create method above receives input from the new.html.erb file. The new method renders the form and the create method handles the input and stores the input into the database using Active Record. &lt;/p&gt;

&lt;h2&gt;
  
  
  Model
&lt;/h2&gt;

&lt;p&gt;The model uses Active Record to interact with the database to fetch data or to store data in the database. The controller will use Active record to store the data in an instance variable. We have an example of that in our index method in our books controller. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary of MVC
&lt;/h2&gt;

&lt;p&gt;The controller in a rails application works between the model and the view. This, in theory, allows for a separation of responsibilities in a rails app. &lt;/p&gt;

&lt;p&gt;So when an HTTP request is sent, the controller will call on the correct method to determine whether or not data needs to be retrieved from the model via Active Record or will it post data. After this is determined, the controller will have the correct view displayed to the user. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>What is Active Record? (Quick Intro)</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Mon, 24 Feb 2020 13:23:25 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-active-record-quick-intro-2p3j</link>
      <guid>https://dev.to/aweysahmed/what-is-active-record-quick-intro-2p3j</guid>
      <description>&lt;p&gt;In this blog post, I'll introduce you to Active Record which is used in the Rails framework. &lt;/p&gt;

&lt;p&gt;Ruby on Rails uses a software design pattern called Model-View-Controller (MVC) and Active Record is part of the model in MVC. This means that Active Records works with your database. &lt;/p&gt;

&lt;h1&gt;
  
  
  Object Relational Mapping (ORM)
&lt;/h1&gt;

&lt;p&gt;Active Record is an ORM. Using an ORM allows a developer to use the language they are working with to query a database without having to use MySQL, PostgreSQL or other databases query commands. &lt;/p&gt;

&lt;p&gt;Active record maps the class name to a table in a database and each row to an instance of the class. &lt;/p&gt;

&lt;p&gt;This is part one of the intro into Active Record and it will be a series. The following blog posts in this series will cover. &lt;/p&gt;

&lt;ul&gt;
 &lt;li&gt;Intro Part II&lt;/li&gt;
 &lt;li&gt;Migrations&lt;/li&gt;
 &lt;li&gt;Validations&lt;/li&gt;
 &lt;li&gt;Callbacks&lt;/li&gt;
 &lt;li&gt;Associations&lt;/li&gt;
 &lt;li&gt;Queries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far what you need to know about Active Record is that it is an ORM. It allows you to interact with a database using the language that you are building your application with. &lt;/p&gt;

&lt;p&gt;In part two, we will look at some examples of how Active Record allows you to interact with your database. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Test-Driven Development (TDD) tutorial using Rspec and Ruby on Rails.</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Tue, 11 Feb 2020 14:51:43 +0000</pubDate>
      <link>https://dev.to/aweysahmed/test-driven-development-tdd-tutorial-using-rspec-and-ruby-on-rails-73h</link>
      <guid>https://dev.to/aweysahmed/test-driven-development-tdd-tutorial-using-rspec-and-ruby-on-rails-73h</guid>
      <description>&lt;h1&gt;
  
  
  What is Test-Driven Development (TDD)?
&lt;/h1&gt;

&lt;p&gt;It is a software development process where the development of your code is moved forward by the tests you write. It may seem counter-intuitive but with TDD you write tests that will fail and then you write code that will pass those tests. &lt;/p&gt;

&lt;p&gt;In Kent Beck's book "Test-Drive Development By Example" he states these two rules. &lt;/p&gt;

&lt;ol&gt;
 &lt;li&gt;Don't write a line of new code unless you first have a failing automated test&lt;/li&gt;
 &lt;li&gt;Eliminate Duplication&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These rules bring us to Red/green/refactor method of TDD. The first test that you create will fail and then you write the necessary code to pass the test. Red indicates a fail and green means you passed. However, getting to green does not mean you are done. After you have passed the test, you will need to refactor what you have written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why test first?
&lt;/h2&gt;

&lt;p&gt;Testing first can minimize bugs and the need to refactor as your codebase increases in size and complexity. Starting off with well written and tested code can also save cost and time. &lt;/p&gt;

&lt;p&gt;Tests can also serve as documentation for your feature in an application. Other developers on the team can read your tests and easily figure out what your code is looking to achieve. &lt;/p&gt;

&lt;p&gt;By creating the tests first, you can ensure that the function that you are testing works the way it is intended to work and avoid any surprises further down the line. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is Rspec?
&lt;/h3&gt;

&lt;p&gt;It is a domain-specific language written in Ruby and specifically designed for test writing.  It is used for testing ruby applications. However, there is also a gem for testing ruby on rails applications called RSpec-rails. &lt;/p&gt;

&lt;p&gt;In this tutorial, we will be using the rails testing version of Rspec. &lt;/p&gt;

&lt;h3&gt;
  
  
  The setup
&lt;/h3&gt;

&lt;p&gt;To get this tutorial started let create a rails application. Let's open up the terminal and run this command in the directory in which you would like to create the app. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 new [name-of-your-app] -T

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

&lt;/div&gt;

&lt;p&gt;The reason we are using the -T flag is to prevent the rails Mini-test from being installed since we will be using Rspec in this tutorial. &lt;/p&gt;

&lt;p&gt;Make sure you add the gem to the development and test section fo the gem file. Your gem rpsec-rails version may differ. At the time of writing this tutorial, 3.9 was the latest stable release. &lt;/p&gt;

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

&lt;span class="c1"&gt;#gemfile&lt;/span&gt;
&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'byebug'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;platforms: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:mri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:mingw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:x64_mingw&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'rspec-rails'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'~&amp;gt; 3.9'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;After that, you'll need to type run this command in the terminal.&lt;/p&gt;

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

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

&lt;/div&gt;

&lt;p&gt;Then you'll need to run this command &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 generate rspec:install

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

&lt;/div&gt;

&lt;p&gt;Now that we are set up for testing let's go over some of the RSpec syntaxes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Important keywords
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;describe&lt;/li&gt;
&lt;li&gt;context&lt;/li&gt;
&lt;li&gt;it&lt;/li&gt;
&lt;li&gt;expect&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is describe?
&lt;/h2&gt;

&lt;p&gt;The describe keyword contains a group of tests called examples in Rspec. &lt;br&gt;
The keyword takes a class name or a string argument. &lt;/p&gt;

&lt;p&gt;The examples will be enclosed in a ruby do/end block. &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;describe&lt;/span&gt; &lt;span class="no"&gt;Pet&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 

&lt;span class="k"&gt;end&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;In the above example, we are passing a class name as an argument. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is context?
&lt;/h3&gt;

&lt;p&gt;The context keyword is similar to describe in that it can take a class name or string as an argument and it also encloses examples in a ruby do/end block. It is optional to use but it can help add more information to your test. &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;describe&lt;/span&gt; &lt;span class="no"&gt;Pet&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 
 &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="s2"&gt;"validations"&lt;/span&gt; &lt;span class="k"&gt;do&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;h2&gt;
  
  
  What is "it" and expect?
&lt;/h2&gt;

&lt;p&gt;The Rspec keyword "it" is used to introduce a test or example. &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;describe&lt;/span&gt; &lt;span class="no"&gt;Pet&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 
 &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="s2"&gt;"validations"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"ensure the presence of a name"&lt;/span&gt; &lt;span class="k"&gt;do&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;The expect keyword outlines an expectation from the test results. &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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"actual value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Let's see how this works as an example. Were going to create a Pet model&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 generate model name:string

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

&lt;/div&gt;

&lt;p&gt;This command will also create the file needed to run the RSpec tests. It will be located in this location in your app&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;spec&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;pet_spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rb&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now were are ready to start with TDD. Our Pet model requires a name. We want to validate the name before it is saved to the database.&lt;/p&gt;

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

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rails_helper'&lt;/span&gt;

&lt;span class="no"&gt;RSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt; &lt;span class="no"&gt;Pet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;type: :model&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="s1"&gt;'validations'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"ensures the presence of name"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pet&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="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&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;The test above expects that the initialized pet object will return false because we passed in an empty string to the name attribute but we do not have any validations in place so we will get true when our expectation is false.&lt;/p&gt;

&lt;p&gt;Since we only have one test that we are running, we can type in RSpec into the command line. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4x7o613drllh7cox7yny.png" 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%2Fi%2F4x7o613drllh7cox7yny.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To get your test results to look like the above, you'll need to add --format documentation to your .rspec file which is located at the root level of your application. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test has failed and we have our first red in the red/green/refactor of TDD. We need to prevent empty strings from been saved to the name attribute and turn this test green. &lt;/p&gt;

&lt;p&gt;We'll need to head to our the Pet model in our application add a name validation. &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;Pet&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&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 we run the test, we should expect it to pass and give us the green that we are looking for. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw8jqzfnda6itw2n0dnsa.png" 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%2Fi%2Fw8jqzfnda6itw2n0dnsa.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this test, I specified the location of the RSpec file that I wanted to test. This will come in handy when you are testing a large application and you don't want to test every test that has been written. &lt;/p&gt;

&lt;p&gt;Our test has now passed and for this example, we won't need to conduct a refactor for the pet model validations. &lt;/p&gt;

&lt;p&gt;The next test we will write will ensure that the name of the pet has at least two characters.&lt;/p&gt;

&lt;p&gt;So let's start by writing a test that will fail.&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;it&lt;/span&gt; &lt;span class="s2"&gt;"ensures that name has two or more characters"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pet&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="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"j"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&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;Here is the output from the command line. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz334fr5o1wjcropas4o3.png" 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%2Fi%2Fz334fr5o1wjcropas4o3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have our failing test now so now we need to create the validation that will put us in the green. &lt;/p&gt;

&lt;p&gt;In our pet.rb file, we'll include this &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;validates&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;length: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;minimum: &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;Using the length Active Record helper we can make sure that only names with 2 or more characters can get saved. &lt;/p&gt;

&lt;p&gt;Let's run the test again and see if we get the passed. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feqfdyf8ajpllhdxdjrek.png" 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%2Fi%2Feqfdyf8ajpllhdxdjrek.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have passed the test and this example will not require a refactor.&lt;/p&gt;

&lt;p&gt;For this tutorial, we will create one more test to ensure that valid names will be accepted. &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;it&lt;/span&gt; &lt;span class="s2"&gt;"ensures that valid name will be saved"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pet&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="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Stella"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be_valid&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;In this scenario, I don't expect it to fail but I'm going to add just to demonstrate what you can test for. &lt;/p&gt;

&lt;p&gt;Here is the output of the final test. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqsixedtstgqfpm844l2f.png" 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%2Fi%2Fqsixedtstgqfpm844l2f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope after this tutorial you have a better understanding of TDD and Rspec. There will be another TDD and Rspec tutorial that will encompass refactoring and other Active Record validation helpers. &lt;/p&gt;

</description>
      <category>tdd</category>
      <category>ruby</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is Redis?</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Thu, 06 Feb 2020 14:04:45 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-redis-3gk0</link>
      <guid>https://dev.to/aweysahmed/what-is-redis-3gk0</guid>
      <description>&lt;p&gt;As you continue to learn more about software development, you'll start to hear about more Frameworks, libraries, tools and databases. &lt;/p&gt;

&lt;p&gt;Sometimes this can feel overwhelming but rest assured you don't have to know and master everything in software development. No one has or will. &lt;/p&gt;

&lt;p&gt;However, it doesn't mean you cannot try and learn as much as possible. &lt;/p&gt;

&lt;p&gt;Once you start working on a larger project with a bigger development team, you may encounter Redis. &lt;/p&gt;

&lt;h1&gt;
  
  
  What is Redis?
&lt;/h1&gt;

&lt;p&gt;It is an in-memory data structure store. It can be used as a database or cache. It can be used as a message broker as well but this is beyond the scope of this blog post. &lt;/p&gt;

&lt;p&gt;When used as a database it would be considered a NoSQL database that uses Keys and Values. It supports these data structures.&lt;/p&gt;

&lt;ul&gt;
 &lt;li&gt;strings&lt;/li&gt;
 &lt;li&gt;hashes&lt;/li&gt;
 &lt;li&gt;lists&lt;/li&gt;
 &lt;li&gt;sets&lt;/li&gt;
 &lt;li&gt;sorted sets&lt;/li&gt;
&lt;li&gt;bitmaps&lt;/li&gt;
&lt;li&gt;sorted sets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the main appeals of using Redis is its speed. Redis data is kept in-memory whereas other databases store data on disk or on SSDs.This allows Redis to access information much quicker. Redis will only store data on disk when it requires persistence. &lt;/p&gt;

&lt;p&gt;Redis contains many other features that we will explore deeper in future blog posts. &lt;/p&gt;

&lt;p&gt;I hope this blog post has given you enough information to know what Redis is?&lt;/p&gt;

&lt;p&gt;If not, feel free to ask me and I will gladly provide more information. &lt;/p&gt;

</description>
      <category>redis</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building and Publishing my first Ruby Gem. (Also a tutorial)</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Mon, 27 Jan 2020 13:57:45 +0000</pubDate>
      <link>https://dev.to/aweysahmed/building-and-publishing-my-first-ruby-gem-also-a-tutorial-25j8</link>
      <guid>https://dev.to/aweysahmed/building-and-publishing-my-first-ruby-gem-also-a-tutorial-25j8</guid>
      <description>&lt;p&gt;I have been looking forward to building a Ruby gem for quite some time. I have used them in my projects and wanted to see what is required to build one. &lt;/p&gt;

&lt;p&gt;I'm also excited about sharing this process so that you can also build a Ruby Gem. If you choose to do so.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Gem?
&lt;/h1&gt;

&lt;p&gt;A Ruby Gem is a software package that you can download using the command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem install [gem-name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby Gems extend your code and can reduce the amount of code you will have to write. They can work directly with your source code or help automate some tasks. &lt;/p&gt;

&lt;p&gt;Here are examples of Gems that you can install. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rails&lt;/li&gt;
&lt;li&gt;Bundler&lt;/li&gt;
&lt;li&gt;Rspec&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;I decided to build a Ruby gem that contains two Class functions that calculate the future value of an asset and the other function calculates the amount needed to have a certain value in the future. &lt;/p&gt;

&lt;p&gt;Here is the link to my gem &lt;a href="https://rubygems.org/gems/time_value_money"&gt;time_value_money&lt;/a&gt; so that you can look at the source code of a completed gem.&lt;/p&gt;

&lt;p&gt;The first thing we will need to do is create an account at &lt;a href="https://www.rubygems.org"&gt;rubygems.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you have done that, you will need to open your terminal and type this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle gem [gem-name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output to your terminal should look like the below image. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t-GNxVSQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/5zk3wn86vim2vghbk760.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t-GNxVSQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/5zk3wn86vim2vghbk760.png" alt="Alt Text" width="767" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next thing you'll need to do is to push this up to your git account. You'll need your git source code URI to add to your README.md file. &lt;/p&gt;

&lt;p&gt;The ReadME.md file will also have a few changes that need to be made. &lt;/p&gt;

&lt;p&gt;We'll also need to create a CHANGELOG.md file in the root of your project. This will be used to log any changes to your gem file. The changes can come from either you or others. &lt;/p&gt;

&lt;p&gt;Here is the CHANGELOG.md from my gem file to give you an idea of what needs to be included. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PIDtmDqg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/n5sf0m7lx55rrn7xyelc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PIDtmDqg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/n5sf0m7lx55rrn7xyelc.png" alt="Alt Text" width="767" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating the CHANGELOG file, you'll need to open up the gemspec file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[gem-name].rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tPLERvfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/f7x5csjdkp02ronmds22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tPLERvfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/f7x5csjdkp02ronmds22.png" alt="Alt Text" width="705" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The sections Marked TODO will need to be changed. &lt;/p&gt;

&lt;p&gt;You will also want to remove or comment out these lines if you plan on posting it to rubygems.org.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5pyM9J8c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/joqxw5mjt634ufa29qxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5pyM9J8c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/joqxw5mjt634ufa29qxb.png" alt="Alt Text" width="703" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To add functionality to your gem, you'll need to go to this file located at&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lib/[your-gem-name]/[your-gem-name].rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have added your code, you'll need to test it to make sure that it is working. &lt;/p&gt;

&lt;p&gt;Once you have done that, you'll need to get your gem file ready for deployment. &lt;/p&gt;

&lt;p&gt;You'll need to run this command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem build [gem-name].gemspec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you'll need to sign in to rubygems in the command line. This can be done by typing&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;gem signin&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 and follow the login instructions.&lt;/p&gt;

&lt;p&gt;Then you can finally push your gem to ruby gems by typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem push [name-of-gem]-0.1.0.gem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations, you have published your first Ruby Gem. &lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>ruby</category>
    </item>
    <item>
      <title>What is Flutter?</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Tue, 21 Jan 2020 14:57:12 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-flutter-10pl</link>
      <guid>https://dev.to/aweysahmed/what-is-flutter-10pl</guid>
      <description>&lt;p&gt;This weekend I decided to take the dive and build a mobile app. I always assumed that I would use React Native because I use React at work and it would be an easier transition. &lt;/p&gt;

&lt;p&gt;However, I have also been curious about trying something different and something with the ability to work on ios, android, and web. &lt;/p&gt;

&lt;p&gt;So I decided to build this app using Flutter over the weekend. The app is a simple future value calculator that determines what the value of an asset will be in the future. &lt;/p&gt;

&lt;h1&gt;What is Flutter?&lt;/h1&gt;

&lt;p&gt;Flutter is an open-source UI toolkit that can be used for mobile, web or desktop and is created from a single codebase. So you write your code once and it will run on ios, android, and desktop. &lt;/p&gt;

&lt;h2&gt;Dart Programming Language&lt;/h2&gt;

&lt;p&gt;Flutter uses the Dart programming language because it can be used to develop on different platforms (ios, android, web). It also has a fast development cycle by making use of hot reload. Any changes you make to your flutter application are automatically reflected in UI instantly. &lt;/p&gt;

&lt;p&gt;Dart will be familiar to you if you have used JavaScript, Swift or Java. &lt;/p&gt;

&lt;p&gt;Variables do not have to have their datatypes explicitly stated. However, when you create a function the parameters should have their datatypes specified. &lt;/p&gt;

&lt;p&gt;Let's look at some sample dart code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;//function&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;Every app has a main() function and it has the keyword void to indicate that it isn't returning anything.&lt;/p&gt;

&lt;p&gt;Let's look at how we would declare a function.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;addNum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Those were a few features of Dart. Head to the &lt;a href="https://www.dart.dev"&gt;Dart&lt;/a&gt; to learn more about dart. &lt;/p&gt;

&lt;p&gt;Here is a link to the Flutter App I built this weekend. &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9-wwsHG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/AweysAhmed"&gt;
        AweysAhmed
      &lt;/a&gt; / &lt;a href="https://github.com/AweysAhmed/flutter-app"&gt;
        flutter-app
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &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;how_much&lt;/h1&gt;

&lt;/div&gt;

&lt;p&gt;A new Flutter project.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting Started&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;This project is a starting point for a Flutter application.&lt;/p&gt;

&lt;p&gt;A few resources to get you started if this is your first Flutter project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://flutter.dev/docs/get-started/codelab" rel="nofollow"&gt;Lab: Write your first Flutter app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://flutter.dev/docs/cookbook" rel="nofollow"&gt;Cookbook: Useful Flutter samples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For help getting started with Flutter, view our
&lt;a href="https://flutter.dev/docs" rel="nofollow"&gt;online documentation&lt;/a&gt;, which offers tutorials,
samples, guidance on mobile development, and a full API reference.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/AweysAhmed/flutter-app"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
 I still have some refactoring to conduct but the UI and the ability to calculate the future value are complete. Let me know if you have any questions or suggestions for the app. 

&lt;p&gt;I'll post about Flutter after I refactor and start the process of launching the app. &lt;/p&gt;

</description>
      <category>flutter</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Object Orientated Programming (Part-three) Polymorphism?</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Mon, 20 Jan 2020 14:29:03 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-object-orientated-programming-part-three-polymorphism-gc2</link>
      <guid>https://dev.to/aweysahmed/what-is-object-orientated-programming-part-three-polymorphism-gc2</guid>
      <description>&lt;p&gt;This blog post will cover the object-orientated principle of polymorphism. Some of the concepts in polymorphism will also relate to inheritance. &lt;/p&gt;

&lt;h1&gt;What is Polymorphism?&lt;/h1&gt;

&lt;p&gt;Polymorphism means many forms. In OOP, this allows us to create a Superclass with a method that is inherited by its subclasses but the method in the subclass will perform a different action.&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;Mammal&lt;/span&gt;
 &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;live_birth&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"We give birth to live animals"&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;Platypus&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Mammal&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;live_birth&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"We lay eggs."&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;span class="n"&gt;duckBill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Platypus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="n"&gt;duckBill&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;live_birth&lt;/span&gt;

&lt;span class="c1"&gt;#output &lt;/span&gt;
&lt;span class="s2"&gt;"We lay eggs"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The above method of using polymorphism uses inheritance. The superclass and the subclass both contain the method live_birth but the Platypus class has changed the output of the method. This allows flexibility in your code. &lt;/p&gt;

&lt;p&gt;In our Mammal class, it made sense to inherit that method because most mammals do give birth to live young, however, for the platypus class polymorphism has allowed us to override that method. &lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;Polymorphism allows a subclass to use a method that is inherited from the superclass and to change it to meet the needs of the subclass.&lt;/p&gt;

&lt;p&gt;The next blog post will still be about polymorphism but also include super and Modules.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>oop</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is a TypeScript Interface?</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Mon, 13 Jan 2020 15:33:29 +0000</pubDate>
      <link>https://dev.to/aweysahmed/what-is-a-typescript-interface-44oj</link>
      <guid>https://dev.to/aweysahmed/what-is-a-typescript-interface-44oj</guid>
      <description>&lt;h1&gt;TypeScript Interface&lt;/h1&gt;

&lt;p&gt;Interfaces in TypeScript create a structure of what the object should contain. Think of how we created variables in the previous post and then stated what type the variable would accept. Interfaces allow us to do this with an object. &lt;/p&gt;

&lt;p&gt;Let us imagine that we have a recreational basketball league and we want teams to register their players. However, we want to ensure that teams register their players with specific information. What we can do is create a basketball player interface and define its structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;BasketballPlayer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;jerseyNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;currentTeam&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have created a Basketball player interface, let's see how we can use this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;registerPlayer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registerPlayer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BasketballPlayer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registerPlayer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;kingJames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lebron&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;jerseyNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;All&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;currentTeam&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lakers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;registerPlayer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kingJames&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;




&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nl"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lebron&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;jerseyNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;All&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Using the type of BasketballPlayer ensures that the correct information is submitted by all the teams and adds consistency to the application. &lt;/p&gt;

&lt;p&gt;Any attempt to add other properties or omitting them will result in an error when being compiled. &lt;/p&gt;

&lt;p&gt;We can also included properties that are optional. Let's assume that in our recreational league adding a nickname is optional. We could add that by adding ? after the property. &lt;/p&gt;

&lt;p&gt;Here is what our BasketballPlayer type would like like with an optional nickname.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;BasketballPlayer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;jerseyNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;currentTeam&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;nickName&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nickname is now optional and allows some flexibility on adding properties that are not imperative. &lt;/p&gt;

&lt;p&gt;In our next blog post, I'll review how we can use Interfaces with Classes. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Framework vs Library (What are they?)</title>
      <dc:creator>Aweys Ahmed</dc:creator>
      <pubDate>Fri, 10 Jan 2020 18:52:14 +0000</pubDate>
      <link>https://dev.to/aweysahmed/framework-vs-library-what-are-they-3a1g</link>
      <guid>https://dev.to/aweysahmed/framework-vs-library-what-are-they-3a1g</guid>
      <description>&lt;p&gt;Using a Framework or library or both can help you build out applications. They are different despite these terms being using interchangeably. &lt;/p&gt;

&lt;h2&gt;What is a Library&lt;/h2&gt;

&lt;p&gt;A library is a collection of reusable code that has been created somewhere else. You end up calling these methods onto your code. The fact that you call the methods from the library into your application is a key differentiator from a framework. &lt;/p&gt;

&lt;p&gt;Let's use React's render method to demonstrate how a library would be used in your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Demo&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;We&lt;/span&gt; &lt;span class="nx"&gt;just&lt;/span&gt; &lt;span class="nx"&gt;used&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we used the React's render method to return the jsx code that we have written. &lt;/p&gt;

&lt;p&gt;We use a library for the predefined methods and features on the code that we create. &lt;/p&gt;

&lt;h2&gt;What is a Framework?&lt;/h2&gt;

&lt;p&gt;A Framework creates a skeleton or scaffold that calls on the code that you have written. Once you implement the Framework, you fill in the rest of the code with your code. The framework will then call on your code. &lt;/p&gt;

&lt;p&gt;Let's use Rails which is a ruby framework to demonstrate 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticlesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="vi"&gt;@articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&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;Let's review the Ruby Controller above to see what the rails framework is doing. &lt;/p&gt;

&lt;p&gt;I used rails to create this Articles controller and we have one method called index. The name of the method corresponds with a URL. When a user heads to the correct URL, rails will call the index method and run the code we defined in that method. So in the above example, every article will be stored in the @articles instances variable. &lt;/p&gt;

&lt;p&gt;A framework calls on your code to execute whereas you as the developer call on methods from a library.&lt;/p&gt;

&lt;h2&gt;Inversion of Control&lt;/h2&gt;

&lt;p&gt;When you are using a library, you create your code and call on the library as you need it. With a Framework we have an Inversion of control and the Framework calls on your code to execute the unique features of your code. &lt;/p&gt;

&lt;p&gt;Here is a quick analogy for Library vs Framework.&lt;/p&gt;

&lt;p&gt;If you wanted to build a car from scratch you would need tools to get the job done. The tools that you would use to build the car from scratch would be your library. &lt;/p&gt;

&lt;p&gt;If you were to use a Framework to construct a car, you would start with an already constructed chassis or vehicle frame and you would put in the unique finishing touches. &lt;/p&gt;

&lt;p&gt;I hope this helped you getting a better understanding of Framework and Library. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
