<?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>Forem: Dawn Cronin</title>
    <description>The latest articles on Forem by Dawn Cronin (@dawncronin).</description>
    <link>https://forem.com/dawncronin</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%2F267354%2F4aa624b6-f854-47b0-bb62-2bf3d47b4318.jpg</url>
      <title>Forem: Dawn Cronin</title>
      <link>https://forem.com/dawncronin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dawncronin"/>
    <language>en</language>
    <item>
      <title>Bubble Sort in Javascript</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Tue, 14 Apr 2020 01:29:31 +0000</pubDate>
      <link>https://forem.com/dawncronin/bubble-sort-in-javascript-43je</link>
      <guid>https://forem.com/dawncronin/bubble-sort-in-javascript-43je</guid>
      <description>&lt;p&gt;Bubble Sort is a common sorting algorithm that makes multiple passes of an array, each time moving the largest value left in the unsorted section the the end. It's called "bubble sort" because numbers bubble up to their correct position during each pass. &lt;/p&gt;

&lt;p&gt;Bubble sort isn't the fastest algorithm, it takes on average/most n^2 time, and at a minimum n time. It is very space and memory efficient, only need constant space, and it does not use recursion. &lt;/p&gt;

&lt;p&gt;It's important to visualize bubble sort first. We will walk through an example array with 5 numbers. We will highlight the number we are currently looking at in green, and use the double arrow to represent a value comparison. I've also added a partition that moves from the back of the array forward as we sort more elements.&lt;/p&gt;

&lt;p&gt;To start, we look at the value 7 at the start of the array. We compare that with 3, and 7 is the higher value. We can swap the two values, and continue looking at 7. &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%2Fwxpmpkejewecgf6ro75g.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%2Fwxpmpkejewecgf6ro75g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then can compare 7 with 1. 7 is greater than 1, so we swap values. &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%2Facpjq933shnws6thczkz.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%2Facpjq933shnws6thczkz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We repeat this process with 4, and then 5, until 7 has reached its rightful spot at the end of the array. &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%2Flf47jvdfp4pc8nk80580.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%2Flf47jvdfp4pc8nk80580.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We return to the start of the array, where we can compare 3 with 1. 3 is moved to the second position.&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%2Fv9icq4m7n4j0egfgf7qi.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%2Fv9icq4m7n4j0egfgf7qi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3 is compared to 4, and this time, it does not change positions. Instead, we change our focus to 4. &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%2F2p7k0ckwoqvdfloxb99w.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%2F2p7k0ckwoqvdfloxb99w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4 is compared with 5. 4 stays in the same position, and we can move our partition up one spot. We can continue this process to finish sorting the array. &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%2Fqtqfrezdmyocj8nwlzmg.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%2Fqtqfrezdmyocj8nwlzmg.png" alt="Alt Text"&gt;&lt;/a&gt;&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%2Fvq8e2lt92khdgj7lfpfr.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%2Fvq8e2lt92khdgj7lfpfr.png" alt="Alt Text"&gt;&lt;/a&gt;&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%2F6rd7thdslvmz6nrtt5mn.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%2F6rd7thdslvmz6nrtt5mn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to sort the array, we needed to have two loops - one that move the partition forward, and one that makes a pass on the array. We can easily translate that to javascript, and write code in the second loop to swap values. I've written out a simple javascript version of bubbleSort below.&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;function&lt;/span&gt; &lt;span class="nf"&gt;bubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&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;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
                &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;save&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;save&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Writing Simple Tests in Ruby</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Mon, 06 Apr 2020 20:03:32 +0000</pubDate>
      <link>https://forem.com/dawncronin/adding-simple-tests-in-ruby-53mc</link>
      <guid>https://forem.com/dawncronin/adding-simple-tests-in-ruby-53mc</guid>
      <description>&lt;p&gt;When I first started with Ruby, I found testing quite intimidating! When doing practice problems, there was always a behind the scenes test suite. How do these tests work? What's the purpose? How can I write my own tests?&lt;/p&gt;

&lt;p&gt;Testing and automation is something that is a part of any production level application. Even if you aren't responsible for writing the tests, you will be responsible for understanding how they work, and how to pass them. I'll be going over some very basic parts of using the gem RSpec with plain ruby. This is intended to give some insight on what the tests are doing, and get you started with writing your own tests!&lt;/p&gt;

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

&lt;p&gt;RSpec is a testing tool for for Ruby applications. It is considered a behavior driven development framework. The tests look at a ruby object or class, and describes what that object should be doing. It is intended to be very intuitive, like the rest of ruby. It's used in a lot of ruby and rails enterprise level applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick start
&lt;/h3&gt;

&lt;p&gt;You can integrate RSpec into your application in many different manners. I'll be going over how to use the rspec/autorun gem, which allows us to run the test suite with the ruby command in our terminal.&lt;/p&gt;

&lt;p&gt;Since we are doing test driven development, we can write our tests before writing our application. We can use the idea of creating a calculator class in ruby as our guideline. &lt;/p&gt;

&lt;p&gt;When ever you need a ruby gem, you need to 'require' it. Create a new file called 'test.rb', and add this line to the top:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rspec/autorun'&lt;/span&gt;

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



&lt;p&gt;Now we have access to the RSpec library in this file. If we are testing a calculator, we want to make sure that it can add, subtract, multiply, and divide. If we have an object of the class Calculator, that object will have methods for all of the operations. In RSpec, all tests have a 'describe' block, and an 'it' block. For adding, we are describing the class Calculator, and it should "add two numbers". Then, we need to finish with our statement that will tell us if the test is passing.  We expect that when we create a new calculator, and add 3 and 4, that it will give us 7.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="no"&gt;Calculator&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;"add two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Calculator&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&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;Our first test is written! If we try running this right now, it will throw an error since we haven't actually defined the Calculator class. In a separate file called 'calculator.rb', we can write out our calculator class. Let's just define an empty class for now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&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 need to make our Calculator class available to our testing file. Add the require relative line to the test.rb file. It should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rspec/autorun'&lt;/span&gt;
&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s1"&gt;'./calculator.rb'&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="no"&gt;Calculator&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;"add two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Calculator&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&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;If you run "ruby test.rb" in your terminal now, you'll get the tests to run, and see this error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nDN01-8R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rxzwcs1iicwx82ttf8n6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nDN01-8R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rxzwcs1iicwx82ttf8n6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great, we can begin our test driven development! We see that there is an undefined method called 'add'. Let's add that to our calculator class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&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 our error looks like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lDeBID_4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g9mfacqohvf4qdfb1cl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lDeBID_4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g9mfacqohvf4qdfb1cl5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We expected the add method to return 7 when passed 3 and 4, instead it returned nil. Let's add input for 2 numbers, and add them in our add method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, let's run our tests! You should now see no failures in your tests. Congrats, you just worked on test driven development. To round out the calculator application, I'm going to add tests for subtract, divide and multiply. Since these are all part of the Calculator class, we can include all of these as separate "it" blocks in our describe. I'll introduce another part of the tests as well. Since we are using the new calculator instance in all of our tests, we can declare it with the let key word before our first "it" block. Now we can just call 'calc' anywhere to use our new calculator. I've included all of the tests below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rspec/autorun'&lt;/span&gt;
&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s1"&gt;'./calculator.rb'&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="no"&gt;Calculator&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:calc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;Calculator&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;it&lt;/span&gt; &lt;span class="s2"&gt;"add two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&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;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"subtracts two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&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;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"multiplies two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&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;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"divides two numbers"&lt;/span&gt; &lt;span class="k"&gt;do&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;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="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="mi"&gt;6&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;If you run all of these new tests without updating the calculator application, you will have test failures. Try writing the methods in the Calculator Class on your own to get all the tests to pass. Your final calculator class should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt; 

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&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;subtract&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;num2&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;multiply&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num2&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;num2&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 you found this easy, then you are ready to start writing your own tests! If this still seems convoluted, don't worry! This is just an overview. A good exercise would be to follow the test format to write tests for other calculator functions, like squaring or factorials. Then, try to get your tests to pass by modifying the Class. &lt;/p&gt;

&lt;p&gt;I hope this helps break down how to start writing your own tests in Ruby!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>testing</category>
    </item>
    <item>
      <title>Local Storage (and JWT)! Browser Storage Part 3</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Mon, 30 Mar 2020 19:39:25 +0000</pubDate>
      <link>https://forem.com/dawncronin/local-storage-and-jwt-browser-storage-part-3-p4e</link>
      <guid>https://forem.com/dawncronin/local-storage-and-jwt-browser-storage-part-3-p4e</guid>
      <description>&lt;p&gt;This is part 3 of my browser storage series, so please take a look at part 1 and 2 if you have not seen them yet! &lt;/p&gt;

&lt;p&gt;As covered earlier in the series, local storage is a way to store information about a user. The information is stored in the user's browser storage on their computer. Local storage differs from cookies by not being sent in the HTTP request. Instead, the frontend can request it by calling on the localStorage object, which is a child of the window. I've written out what the basic local storage methods are in javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;currentUser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dawn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;currentUser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;currentUser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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



&lt;p&gt;With just setItem, getItem and removeItem, you can save information about a user that will persist between sessions. A user can come back the next day, and anything saved by that site in the local storage will remain. This is incredibly useful for staying logged in. It moves the burden of holding the information about the user's session from the backend and server, to the local storage. This can work because instead of the user holding onto a session id, they hold all the information that authorizes them to be using the site. This means that there doesn't need to be a look up of the user for every new http request sent.&lt;/p&gt;

&lt;p&gt;Can't a user just fake this information?&lt;/p&gt;

&lt;p&gt;They could, but that's where signatures come in. We can use JWT (json web tokens) to include a signature from our server based on a secret that only our servers know. This signature is stored in the JWT in the local storage, and can be accessed, sent to the server, and verified without any database calls. The server knows that unless someone else has the secret, the JWT is accurate and this user has certain authorizations. &lt;/p&gt;

&lt;p&gt;Using local storage can be faster than using cookies. Not only does it not need a sessions database, but it also works on different servers. If all of your servers have the same secret, then they can all authorize the user for access of all the other servers. This is especially useful for large, distributed sites, as well as sites with multiple services, such as a bank.&lt;/p&gt;

&lt;p&gt;To learn more about JWT, &lt;a href="//JWT.io"&gt;JWT.io&lt;/a&gt; is the perfect site. Additionally, here is a &lt;a href="https://levelup.gitconnected.com/jwt-auth-in-a-react-rails-app-8a7e6ba1ac0"&gt;walkthrough&lt;/a&gt; on how to implement this yourself in a rails and react environment. &lt;/p&gt;

</description>
      <category>rails</category>
      <category>beginners</category>
      <category>ruby</category>
      <category>react</category>
    </item>
    <item>
      <title>Logging In With Cookies! Browser Storage Pt. 2</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Mon, 23 Mar 2020 20:12:14 +0000</pubDate>
      <link>https://forem.com/dawncronin/logging-in-with-cookies-browser-storage-pt-2-2na6</link>
      <guid>https://forem.com/dawncronin/logging-in-with-cookies-browser-storage-pt-2-2na6</guid>
      <description>&lt;p&gt;Last week, I broke down what is browser storage, and today I'll be covering using cookies to log in. I'll be using specific examples from rails with a walk through on implementation. You can read the &lt;a href="https://dev.to/dawncronin/intro-to-browser-storage-5fph"&gt;browser storage post here&lt;/a&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  Cookies Refresher
&lt;/h3&gt;

&lt;p&gt;Cookies are a way for websites to store data on your browser. More specifically, when you make a http request to a server, you share the site's specific cookies in the request. The server can send a response to the user telling them what to store as a cookie. This data is used to allow you to stay logged in between sessions, or maintain a cart on shopping site.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rails Setup
&lt;/h3&gt;

&lt;p&gt;To implement authentication and login using Rails, we can use some built in features. Rails has a built in method called &lt;a href="https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html"&gt;has_secure_password&lt;/a&gt; that will allow you to store a password digest in your database, and handle the encryption. By default, has_secure_password uses the gem bcrypt for the encryption protocol. You will need to add this line to your gem file ( or un-comment it)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'bcrypt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'~&amp;gt; 3.1.7'&lt;/span&gt;

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



&lt;p&gt;Additionally, for your users table, rather than storing a password in the database, you will need a column on your table called password_digest. You can add a migration to change this on an existing rails app, or you can include it in your initial migrations if you are starting a new one. You do not need a 'password' column on your table, just the password_digest. &lt;/p&gt;

&lt;p&gt;Once you have your migrations and gem file up to date, you can now update your user model to associate the password_digest to the has_secure_password method. Your user model should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;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;validates&lt;/span&gt; &lt;span class="ss"&gt;:username&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="n"&gt;has_secure_password&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Adding the has_secure_password line will add a bunch of methods to your User class, allowing you to store encrypted passwords, and check login credentials. &lt;/p&gt;

&lt;h3&gt;
  
  
  Application and Session Controllers
&lt;/h3&gt;

&lt;p&gt;A session in rails is a built in way to access and change cookies pertaining to a user's logged in session. It abstracts the cookies away into acting like a ruby hash. We will be implementing a new controller to manage the cookies we save for maintaining a user's session. This is called the session controller. &lt;/p&gt;

&lt;p&gt;When we think about the actions we need to take to save a user's data, we will need a way to check the cookies when someone accesses a restricted page, add a new cookie when someone logs in, and delete a cookie when someone logs out.  &lt;/p&gt;

&lt;p&gt;Fist in our ApplicationController, we can have a check to see if in the http request, the cookie or session includes a valid logged in user. We can use this current_user method across all of our controllers now to check who is logged in. The sessions are available to us using hash notation in ruby.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
    &lt;span class="n"&gt;helper_method&lt;/span&gt; &lt;span class="ss"&gt;:current_user&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;current_user&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
          &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&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;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&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;In our new SessionsController, we can include a create which will correlate with logging in, and a destroy that will correlate with logging out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SessionsController&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;create&lt;/span&gt;
      &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;username: &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;:username&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;  
        &lt;span class="n"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:error&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Invalid username or password."&lt;/span&gt;
        &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="s2"&gt;"/login"&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;def&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;
      &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

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



&lt;p&gt;At this point, you need to make sure that your other controllers have the correct access on them, only allowing users that are logged in when necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;    &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="ss"&gt;:require_login&lt;/span&gt;
    &lt;span class="n"&gt;skip_before_action&lt;/span&gt; &lt;span class="ss"&gt;:require_login&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;:create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&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;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;
            &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="s2"&gt;"/users/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
        &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valid?&lt;/span&gt;
            &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
            &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;
            &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="n"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:errors&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;full_messages&lt;/span&gt;
            &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="s2"&gt;"/signup"&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

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

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_params&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password_confirmation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;require_login&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:forbidden&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt; &lt;span class="ss"&gt;:user_id&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You'll need to make sure that your routes match what you have setup, with your login and logout post requests mapping to the sessions controller. Additionally, this method works fine for small scale applications and projects, but for larger scaling, or bigger cookies, there can be issues with using cookies.&lt;/p&gt;

&lt;p&gt;Next week I'll be looking at a specific user of local storage and JTW. Stay tuned!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>beginners</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Intro to Browser Storage</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Mon, 16 Mar 2020 23:52:02 +0000</pubDate>
      <link>https://forem.com/dawncronin/intro-to-browser-storage-5fph</link>
      <guid>https://forem.com/dawncronin/intro-to-browser-storage-5fph</guid>
      <description>&lt;p&gt;Before getting into web development, I had a limited understanding of cookies. I knew that websites stored data about me as cookies, and used that data to customize my user experience. I also knew that when I cleared my cookies, websites forgot this information.&lt;/p&gt;

&lt;p&gt;Then I learned that there was also local storage and session storage, both of which can store data as well. &lt;/p&gt;

&lt;p&gt;What was with all these different storage types?&lt;/p&gt;

&lt;p&gt;To start, let's talk about cookies! Before localStorage or sessionStorage even existed on browsers, there were Cookies. Cookies were designed for browsers to store stateful information on the user's computer. Stateful information might be a cart on an online store, or information about the current user. Cookies are sent in the head of an HTTP request to a given site's sever. If you have previously logged into a site, then the cookies for that site will have information about your account. The server will use this information to tell if you have a valid session. Since cookies are site specific, this works most of the time at protecting your data.&lt;/p&gt;

&lt;p&gt;Cookies can also be used to keep track of your browsing data by third party companies, which is why it can have a bad reputation in the news. This allows for targeted ads based on your browsing history. &lt;/p&gt;

&lt;p&gt;In 2012, most major browsers added localStorage as an option for sites to save information on a users computer. Since the cookies were sent in every HTTP request, it was often slowing down the response times. LocalStorage is only accessible by the client and is not sent in the request to the server. Since this data isn't part of the request, it changes how user sessions need to be saved. Rather than storing user sessions data in a database, a JWT can be used and placed in local storage.&lt;/p&gt;

&lt;p&gt;JWT, or a JSON Web Token, is a standard way to pass JSON data over the web. It is encoded, and can include information about the user that is logged in and their permissions. The JWT does not need to be saved server-side, since it can decode the token to find the user information. &lt;/p&gt;

&lt;p&gt;SessionStorage is similar to localStorage, but it is deleted after each page session. This might hold information that is specific to interactions you've made during a single website visit. &lt;/p&gt;

&lt;p&gt;If you are considering using localStorage or Cookies to store user data, there are trade offs to both on the security side as well. Additionally, how you set up your server will vary greatly on how to you want to save user sessions. I will do another post on how to implement both on Rails. I hope this clears up some confusion!&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>An Ode to Node: An introduction</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Sat, 07 Mar 2020 00:09:51 +0000</pubDate>
      <link>https://forem.com/dawncronin/an-ode-to-node-an-introduction-4455</link>
      <guid>https://forem.com/dawncronin/an-ode-to-node-an-introduction-4455</guid>
      <description>&lt;p&gt;I was having a conversation with some of my colleagues in bootcamp a few months back when we were deciding whether to attend a node.js meetup. It went something like this: &lt;br&gt;
"What even is node? Is it a framework, or a language, or a tool"&lt;/p&gt;

&lt;p&gt;"Google says it's a runtime for javascript"&lt;/p&gt;

&lt;p&gt;"But what does that even mean?"&lt;/p&gt;

&lt;p&gt;" I feel like we can't attend if we don't even know what it is..."&lt;/p&gt;

&lt;p&gt;This might not represent everyone's experience, but knowing about javascript did not give me an insight on what node was all about. &lt;/p&gt;

&lt;p&gt;Let's first start with talking about Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript
&lt;/h3&gt;

&lt;p&gt;Javascript is a scripting language that was created as a way to make webpages dynamic. Before javascript, webpages were static, and didn't change much based on your interaction with them. If you interact with a map, or have information change based on scrolling or time, then you've been witnessing the magic of javascript. &lt;/p&gt;

&lt;p&gt;Javascript is one of the three web technologies, along with HTML and CSS. Javascript is interpreted by the browsers's built in javascript engine, such as chrome's V8. It was originally only built to make webpages more dynamic, so there was no reason to run javascript outside the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node
&lt;/h3&gt;

&lt;p&gt;As the use cases of Javascript expanded, it became desirable to run javascript on the server side. However, this wasn't what javascript was designed to do. If you could use javascript on the server side, you could unify your client and server by only using one language. &lt;/p&gt;

&lt;p&gt;Node was built using chrome's V8 engine, along with other features that were required to interact with a server's file system. It is nearly identical to javascript in the browser, except your event system and global isn't browser based anymore. Basically if you learn javascript, you've already started learning node! So back to the question, What is node? It's a runtime for javascript, based on your computer, rather than a browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  What can I do with Node?
&lt;/h3&gt;

&lt;p&gt;Since node allows you to run javascript on your server, you can do anything you would have done with another server side language, like ruby or python!&lt;/p&gt;

&lt;p&gt;Where Ruby has Rails, Node also has frameworks for building out a backend or API. Express is a light weight web application framework. Similarly, you can build CLIs and programs that interface with the file system.&lt;/p&gt;

&lt;p&gt;To summarize, with node, you can be a full-stack developer, while learning only one coding language. Amazing! What are your thoughts on full-stack javascript development?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Hosting your Rails Projects to Heroku</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Thu, 13 Feb 2020 20:22:22 +0000</pubDate>
      <link>https://forem.com/dawncronin/hosting-your-rails-projects-to-heroku-542</link>
      <guid>https://forem.com/dawncronin/hosting-your-rails-projects-to-heroku-542</guid>
      <description>&lt;p&gt;You've finished a personal project and you feel great! The project looks good, there aren't any bugs, and you want to show everyone. You can share your github link, but it's hard to really gauge a project by just looking at the code. Your non-technical friends will have no idea of the work you just put in, and prospective companies won't spend enough time looking at your project. The solution? Host your project!&lt;/p&gt;

&lt;p&gt;There are many different ways to host your project (for free!) on the internet. If you are hosting a professional/production site, you will want to pay money for a better site. Heroku, for example does not have the ability to handle a lot of traffic or a lot of storage on their base plan. The your app will be quite slow, as it needs to start up when someone requests the url.&lt;/p&gt;

&lt;p&gt;You can host any variety of sites on heroku, including a static rails app, react apps,  and node.js apps. I'll be going over how to host a rails site or rails API, and suggest how to host your front ends as well. &lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Git installed and project initialized in a git repo&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Site and code is ready to be hosted (no bugs or errors) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://signup.heroku.com/"&gt;Create a heroku account&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;Install heroku on your computer&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For Mac users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew tap heroku/brew &amp;amp;&amp;amp; brew install heroku
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Login to heroku through the heroku cli on your terminal. This command will launch a login window for you
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Make sure your api keys are hidden!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you are using database, you can't use SQLite, the default for rails. I suggest using PostgreSQL. If you didn't initiate your application using PostgreSQL, you can follow &lt;a href="https://dev.to/torianne02/making-the-change-from-sqlite3-to-postgresql-ruby-on-rails-2m0p"&gt;Tori's tutorial here to switch over&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you are using active storage, then you will need to use AWS/Azure/GCP to store your data. If you leave it as local, then files will not persist. You can follow the &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html"&gt;Ruby Guide here&lt;/a&gt; on how to configure your cloud storage. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will be using your remote master branch as the branch we push to heroku, so make sure that it is up to date with your latest changes. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Setup
&lt;/h3&gt;

&lt;p&gt;If you've made sure to do all the prerequisites, then you are most of the way there! I suggest when starting a new personal project, to make sure that you are optimizing your project to be hosted, rather than trying to reconfigure things at the end. Here are the steps to get your project hosted:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;From your terminal, cd into your project repo&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you've set up heroku on your machine properly run:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create name-of-project 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After create, you pass in the name of your project. If you simply just use &lt;code&gt;heroku create&lt;/code&gt; then it will receive a default name which you can change later. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Again in your project director terminal, run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku master 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command is pushing your git remote master branch to the heroku remote server. If this doesn't work, (which is usually the failure point) Make sure all of the prereqs are true. The error messages provider usually give you an action item to follow if it is unable to push. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You'll need to make your database now on your heroku app:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku run rails db:migrate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are encountering issues here with postgreSQL, then you can try running heroku pg:reset. Again, if your app is not configured properly, you will need to follow the errors. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you have seeds in your seed file, make your seeds:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku run rails db:seed

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



&lt;ol&gt;
&lt;li&gt;If you have gotten this far, run heroku open to view your app! If you are using rails as a static site, this should be sufficient for running the app. If you are using it as an API, then you should be able see the json if there is no authorization require. If you require auth to see your data, you'll need to fetch it the normal way using your front end, or using postman with a token!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Next Steps:
&lt;/h3&gt;

&lt;p&gt;If you need do add sample data to your site, share it with your friends and family to make posts and comments. If you are using this as an API, then you will need to host your front end too. If your frontend is just vanilla javascript, then you can use github pages. You can go to the frontend directory, settings, then pages and set the branch you would like. Make sure you change your fetch requests to be to your new backend location. The pages will be available on the environment tab on your repo.&lt;/p&gt;

&lt;p&gt;If you are using react, angular, vue or similar for your frontend, then you can choose where to host that as well. You can host the frontend app to heroku as well, following similar steps. I hope that this helps you get started, and have a better project portfolio! &lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title> Recursion for Algorithms in Ruby</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Tue, 21 Jan 2020 02:28:24 +0000</pubDate>
      <link>https://forem.com/dawncronin/ruby-recursion-for-algorithms-587j</link>
      <guid>https://forem.com/dawncronin/ruby-recursion-for-algorithms-587j</guid>
      <description>&lt;p&gt;Recursion is one of the major hurdles new programmers find in learning algorithms. It's not necessarily intuitive, and a requirement to have a grasp on a language. In fact, any recursive algorithm question can be solved using iteration. So why learn recursion? I'll be going over why it's important, what it is, and how to start using in in Ruby.&lt;/p&gt;

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

&lt;p&gt;Recursion is a method to solving a problem where you call on smaller instances of the same problem. While coding, you often call on the current function within itself on a smaller subset of the problem. In iteration, you would call on an index that moves through the smaller instances of the problem. The difficulty here lies in identifying the smaller index. While I'll be talking about recursion in regards to simple ruby functions, it is a topic that can be applied to a lot of complex computer science problems. &lt;/p&gt;

&lt;p&gt;When you call a function inside another function, the functions are added to a call stack, where the outside function awaits the computation of the inner function to finish its computation. Adding the same function to the stack in recursion means at some point you need to hit the smallest instance of the problem. If you don't hit a point where the recursive function call stops, then you will overflow the stack. Recursion is less efficient than iteration, so problems that can be solved easily with iteration should be. If efficiency is important, and it would be hard to use iteration, then tail recursion should be considered. &lt;/p&gt;

&lt;p&gt;In tail recursion, you have to make sure that there is no additional computation to be computed after the recursive call is complete. This can be easily done by returning the recursive call itself, rather than returning an equation that includes a computation using the value of the recursive call. Tail recursion is faster because the interpreter in Ruby will convert it into iteration, rather than having a ridiculous call stack. &lt;/p&gt;

&lt;h2&gt;
  
  
  Importance of Recursion
&lt;/h2&gt;

&lt;p&gt;Recursion is taught in most intro to computer science classes, included in many algorithm books, and is a topic new programmers often struggle with. For many, the idea that recursion can be solved with iteration mean that they don't need to learn how to use it. Recursion as a concept is important in understanding more complex data structures and computer science concepts in general. For example, when solving tree traversal problems, it would be very complex using iteration. With recursion, it become a logical traversal in each layer of the tree. Trees are built by nature to be smaller version of the larger problem, which lends itself nicely to recursion. &lt;/p&gt;

&lt;p&gt;If you aren't planning to learn more complex algorithms, you should still consider understanding how recursion works. The concepts in understanding recursive call relates to many computer science concepts, which I won't cover here. In terms of just the general knowledge, it can be asked in many coding interviews, or even come up in a conversation with manager. If you are looking to be prepared for interviews, you should plan to have recursion under your belt. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby Recursion
&lt;/h2&gt;

&lt;p&gt;In Ruby, recursion is quite simple, but it can look very confusing due to implicit returns. I'll be more robust in my example to show some clarity.&lt;/p&gt;

&lt;p&gt;Problem: Return the sum of a sequence of integers, that are defined by three input numbers: start_num, end_num, and step. If begin_num is greater than the end_num, the function should return 0&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;sequence_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="c1"&gt;# 2 + 4 + 6&lt;/span&gt;

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



&lt;p&gt;If you have seen my previous post on &lt;a href="https://dev.to/dawncronin/ruby-algorithms-moving-past-the-basics-1j7p"&gt;algorithmic thinking&lt;/a&gt;, I'll be following those steps to work through this problem using recursion. The question is asking us to do a geometric sum. The inputs are the start, end and step size, and our output will just be a number. In terms of a solution, I see that I could do it iteratively, but it seams that it would be simpler to do with recursion, since the problem has layers of smaller problems that can be solved with the same equation. &lt;/p&gt;

&lt;p&gt;In ruby, we can use a while loop where the escape case is when the begin_num becomes greater than the end_num. This is the case where we stop adding to the sum. In the loop, we can return the current begin_num plus the same function called with the current begin_num being replaced with the current begin_num plus the step size. The end_num and step will be the same through each function call. When we hit the escape case, we will want to not call the function again, and instead return 0, as instructed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sequence_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;begin_num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end_num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;begin_num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;end_num&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;begin_num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sequence_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;begin_num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end_num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&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;return&lt;/span&gt; &lt;span class="mi"&gt;0&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 solution becomes quite simple, with only 6 lines of code. Since we want to be able to change the inputs, this will work for any step size and begin number. There are no additional variables defined in the function, which allows for clarity in the calculation. In the recursive case, the return in the while loop will await the call of sequence_sum to finish before computing the sum. This means that there will be many function calls waiting for their sum to be returned. When the values finally hit the base case, all of the function calls will return up the value, returning the sum in the top level. &lt;/p&gt;

&lt;p&gt;Learning recursion isn't an easy task. After reading this post, I hope you are feeling more confident in trying problems on your own. This post is not intended to teach you all of recursion, but instead offer you an easier entry point for your own studies.  &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>recursion</category>
      <category>rails</category>
    </item>
    <item>
      <title>Ruby Algorithms - Moving Past the Basics
</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Wed, 01 Jan 2020 18:06:00 +0000</pubDate>
      <link>https://forem.com/dawncronin/ruby-algorithms-moving-past-the-basics-1j7p</link>
      <guid>https://forem.com/dawncronin/ruby-algorithms-moving-past-the-basics-1j7p</guid>
      <description>&lt;p&gt;After figuring out the archetypal intro algorithm - the palindrome problem - I thought I would be ready to take on any coding interview. I could pseudo code, I could talk through the solution, and I could think about variants on the problem. Then I started to look through &lt;em&gt;Cracking the coding interview&lt;/em&gt;, and I realized I was completely over my head. The problems in the book are in the major league, and I'm still playing t-ball in the backyard. &lt;/p&gt;

&lt;p&gt;Luckily, there is this wonderful place called the internet, where many people were willing to give advice on how to get there. Having a strong grasp on your language of choice will be the starting point. I'm most comfortable with Ruby, so that will be the language we go over. I'll be assuming that you've already been exposed to a few algorithms, but you are looking to start working towards the next level. (As mentioned before, you should have worked through the palindrome problem). Ruby is a very user friendly language; It has a method for almost anything you could ever think of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithmic Thinking in Ruby
&lt;/h2&gt;

&lt;p&gt;To start, let's talk about the basic approach to solving an algorithm. This is a model that I usually follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Read the Problem&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand what is required for the inputs and outputs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify in plain english/pseudo code what steps will be needed (what algorithm)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Map the pseudo code to ruby processes and concepts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write Code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimize for time and space&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now lets look at a basic algorithm, and apply these steps:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Write a function that will find all the anagrams of a word from a list. You will be given two inputs: a word and an array of words. You should return an array of all the anagrams, or an empty array if there are none. For example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="n"&gt;anagrams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'racer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'crazer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'carer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'racar'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'caers'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'racer'&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="s1"&gt;'carer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'racer'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read The Problem:&lt;/strong&gt; The question is asking us to find all anagrams for a given word from another list of words. An anagram is a word that is composed of the same letters as the given word.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understand what is required for the inputs and outputs:&lt;/strong&gt; They are giving us two inputs, a string, and an array of strings. They want us to return values from the array in a new array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identify in plain english/pseudo code what steps will be needed:&lt;/strong&gt; This is the tricky part. First, we know we need to iterate through the array of potential anagrams and compare them to the target word. If they are an anagram, we will put those words into a new array. Since we know that the anagrams will be composed of the same letters just in different order, we need to find a way compare just the letters, without order. Sorting comes to mind for that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Map the sudo code to ruby processes and concepts:&lt;/strong&gt; For the iterator, we are selecting elements from an array so let's use &lt;code&gt;select&lt;/code&gt;. For our boolean statement, we want to compare the sorted target word with the sorted element in the array. To sort, we want to sort by chars, so we can use &lt;code&gt;.chars&lt;/code&gt; and &lt;code&gt;.sort&lt;/code&gt;. If they are equal, then select that element. &lt;code&gt;.chars&lt;/code&gt; will treat a string like an array, and &lt;code&gt;.sort&lt;/code&gt; sorts it by the character values. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Write Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;anagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sorted_target_word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;
    &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;sorted_target_word&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&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;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test:&lt;/strong&gt; Our example passes (trust me)!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize for time and space:&lt;/strong&gt; For this example, we weren't looking to optimize. But if we were... the sorting will take some extra time, which should be noted if you were looking to optimize. Sorting takes O(mlog(m)) time (ruby uses quicksort), where m is the number of letters in each word. This multiplied by the number of words, n, gives us n*m*log(m). This might not be most efficient algorithm for this process. Ruby does a lot of work under the hood, so it is easy to forget to think about time and space. A trick that could be useful, if our words were very long, and we had a lot of them, is creating a hash map. You can create a hash of the target word by using &lt;code&gt;target = Hash.new(0)&lt;/code&gt; and then &lt;code&gt;word.each {|char| target[char] += 1}&lt;/code&gt;. For each word in the array you can compare their hash map with the target hash map. Each map takes linear time to set up, and we have to go through the array once, leading us to about O(m*n) time. This is still linear time. Consider that the total operations done on each &lt;strong&gt;letter&lt;/strong&gt; is one constant time insertion into the hash map, and then one constant time comparison with our target word. Hence O(n) time, where n is the number of letters in the word array. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Side note - Memoization
&lt;/h4&gt;

&lt;p&gt;Memoization is where you store expensive function calls to be used later to save time in a computer program. While the hash we are using above isn't quite that concept, it starts exploring the potential of using hashes in algorithms. The easiest example to explain memoization is for factorials. When you want to complete 5!, if you already know 4! then its only a two step process ( 5*4!) . Otherwise, you will have to call 5*4*3*2*1, which can take a lot more time!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips for Better Solving
&lt;/h2&gt;

&lt;p&gt;The steps for algorithmic thinking will only start working for your with lots of practice. I recommend starting on codewars or another similar site. Code wars has different levels of algorithms so you can start where you are comfortable. &lt;/p&gt;

&lt;p&gt;In Ruby, there is a built in method for many common algorithm problems, like sorting and finding in an array. While it's fine to use these in practice, it's also important that you understand how these work. I encourage everyone who is interested in becoming better at algorithms to start thinking through each question, rather than memorizing solutions. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>rails</category>
    </item>
    <item>
      <title>Number Conversions in Ruby</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Wed, 04 Dec 2019 21:03:07 +0000</pubDate>
      <link>https://forem.com/dawncronin/number-conversions-in-ruby-1ned</link>
      <guid>https://forem.com/dawncronin/number-conversions-in-ruby-1ned</guid>
      <description>&lt;p&gt;Beginner rubyists are spoiled while they first learn the language when it comes to type conversions. Ruby has many built in methods for any conversions you could think of, and it allows you to forget about what is actually happening under the hood. I found that even with a strong understanding of ruby syntax, when dealing with floating points I had unexpected results. I'm going to go over simple number conversions in ruby, and then go into some detail on what is actually happening.&lt;/p&gt;

&lt;h3&gt;
  
  
  Built in Methods
&lt;/h3&gt;

&lt;p&gt;Ruby offers simple ways to change numbers from the integer class to the floating point class, and back again. Using &lt;code&gt;to_i&lt;/code&gt;, you can change any number into an instance of the integer class. Likewise, you can use &lt;code&gt;to_f&lt;/code&gt; to convert any number into a floating point number or float class. Floats include decimal places, and are considered inexact number in ruby. I talk about inexect numbers later on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;      &lt;span class="c1"&gt;#10&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;        &lt;span class="c1"&gt;#10.0&lt;/span&gt;
&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;#10&lt;/span&gt;
&lt;span class="no"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;#10.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! You can see that &lt;code&gt;Integer()&lt;/code&gt; and &lt;code&gt;Float()&lt;/code&gt; act the same way as &lt;code&gt;to_i&lt;/code&gt; and &lt;code&gt;to_f&lt;/code&gt;. Ruby also allows you to change strings to numbers using &lt;code&gt;to_i&lt;/code&gt;. This is particularly useful if you are receiving input from a user or a document. When there are non-number characters involved, ruby strips characters after the number, and just leaves you the number. If the numbers fall in the middle or after other characters, ruby will not recognize those numbers, and returns 0. Additionally, if a number in a string has a decimal, it will need to be converted to a float to keep those digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="s2"&gt;"76 Trombones!"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;    &lt;span class="c1"&gt;#76&lt;/span&gt;
&lt;span class="s2"&gt;"76 Trombones!"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;    &lt;span class="c1"&gt;#76.0&lt;/span&gt;
&lt;span class="s2"&gt;"AB72JP"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;           &lt;span class="c1"&gt;#0&lt;/span&gt;
&lt;span class="s2"&gt;"AB72JP"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;           &lt;span class="c1"&gt;#0.0&lt;/span&gt;
&lt;span class="s2"&gt;"32 and 48"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;        &lt;span class="c1"&gt;#32&lt;/span&gt;
&lt;span class="s2"&gt;"32 and 48"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;        &lt;span class="c1"&gt;#32.0&lt;/span&gt;
&lt;span class="s2"&gt;"4.89"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;             &lt;span class="c1"&gt;#4&lt;/span&gt;
&lt;span class="s2"&gt;"4.89"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;             &lt;span class="c1"&gt;#4.89&lt;/span&gt;
&lt;span class="mi"&gt;76&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;                 &lt;span class="c1"&gt;#"76"&lt;/span&gt;

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



&lt;p&gt;Everything seems to behave as expected when converting from strings, and when converting back to a string, we can just use &lt;code&gt;to_s&lt;/code&gt;. Let's look at &lt;code&gt;to_i&lt;/code&gt; again in regards to floats.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mf"&gt;4.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;        &lt;span class="c1"&gt;#4&lt;/span&gt;
&lt;span class="mf"&gt;4.99999&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;    &lt;span class="c1"&gt;#4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For floating points, the &lt;code&gt;to_i&lt;/code&gt; conversion truncates the number, meaning that there is no rounding. Even in the case of 4.99999, it still converts it to 4. What if we want to round to the nearest integer? Ruby has a built in function &lt;code&gt;round()&lt;/code&gt; which allows us to both change floats to integers, and round floats to decimal places. &lt;code&gt;round()&lt;/code&gt; with no argument will round to 0 decimals, which will return an integer type number. Using &lt;code&gt;round(1)&lt;/code&gt; will round to one decimal, and &lt;code&gt;round(2)&lt;/code&gt; will round to two decimals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mf"&gt;4.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;         &lt;span class="c1"&gt;#5&lt;/span&gt;
&lt;span class="mf"&gt;4.99999&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;#5&lt;/span&gt;
&lt;span class="mf"&gt;4.325&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;#4.33&lt;/span&gt;
&lt;span class="mf"&gt;4.99999&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;#5.0&lt;/span&gt;
&lt;span class="mf"&gt;4.2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;           &lt;span class="c1"&gt;#4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this point, I hope you are feeling confident about converting numbers in ruby. Another case of implicit conversions can be in doing math with integers and floats. When you divide two integers using &lt;code&gt;/&lt;/code&gt;, it will be default return a whole number quotient. If you want the remainder, you can use the modulus, &lt;code&gt;%&lt;/code&gt;. If you were hoping for a decimal number, then you will need to have at least one floating point number in the operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ruby Math
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;            &lt;span class="c1"&gt;#4&lt;/span&gt;
&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;            &lt;span class="c1"&gt;#1&lt;/span&gt;
&lt;span class="mf"&gt;17.0&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;          &lt;span class="c1"&gt;#4.25&lt;/span&gt;
&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;#4.25&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;     &lt;span class="c1"&gt;#4.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure that you convert to float &lt;em&gt;before&lt;/em&gt; you divide, otherwise you will end up with the wrong results. I mentioned earlier that float type numbers are &lt;em&gt;inexact&lt;/em&gt;. This makes doing more complicated math difficult using floats. Let's look at an example where this is going to effect your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;         &lt;span class="c1"&gt;#0.09999999999999998&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;             &lt;span class="c1"&gt;#0.1&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;              &lt;span class="c1"&gt;#false&lt;/span&gt;
&lt;span class="mf"&gt;0.4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;           &lt;span class="c1"&gt;#0.6000000000000001&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Why did &lt;code&gt;1 - 0.9&lt;/code&gt; not equal &lt;code&gt;0.1&lt;/code&gt;? This is the inexactness of floats. Becuase of how they are stored, when using them for math, you start to build up errors in the numbers. What is the alternative to using floats? In Ruby, you can use a class called BigDecimal. This is part of Ruby, but you will need to add &lt;code&gt;require "BigDecimal"&lt;/code&gt; to the top of your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"BigDecimal"&lt;/span&gt;

&lt;span class="no"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"76582.124"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#0.76582124e5&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0.9"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;#0.9e0&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;#0.1e1&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;            &lt;span class="c1"&gt;#true&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;#0.1e0&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;            &lt;span class="c1"&gt;#0.1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With BigDecimal, numbers are stored in scientific notation, where the e represents the decimal times 10 to the power after the e. This allows you to work to a precision that isn't available when using floats. &lt;/p&gt;

&lt;p&gt;Hopefully this guide helped you understand ruby number more easily. There are many other nuances you will encounter, but this is a good starting point for most projects.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Companies are Looking for Entry Level Devs with These Technical Skills!</title>
      <dc:creator>Dawn Cronin</dc:creator>
      <pubDate>Wed, 13 Nov 2019 05:38:18 +0000</pubDate>
      <link>https://forem.com/dawncronin/companies-are-looking-for-entry-level-devs-with-these-technical-skills-5c4f</link>
      <guid>https://forem.com/dawncronin/companies-are-looking-for-entry-level-devs-with-these-technical-skills-5c4f</guid>
      <description>&lt;p&gt;&lt;strong&gt;Backstory:&lt;/strong&gt; I'm currently a student at flatiron school, learning full-stack web development. Before flatiron, I was a recruiter for software engineers. As I transition from a technical recruiter to a developer, I am discovering a disconnect between what employers are looking for in candidates, and the skills entry level developers are encouraged to learn. In order to increase my chance of getting a job post-graduation, I decided to check out the real data on what employers are looking for.&lt;/p&gt;

&lt;p&gt;Using LinkedIn job postings, I pulled data from all entry level positions in San Francisco for software engineers. Here are the most asked for languages, frameworks and other tools by percentage of job postings.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;center&gt; Languages: &lt;/center&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Python - 41%
&lt;/h4&gt;

&lt;p&gt;Python has been growing in popularity in recent years, coming in as the second most popular language &lt;a href="https://www.businessinsider.com/most-popular-programming-languages-github-2019-11"&gt;according to Github&lt;/a&gt;. Python is easy to learn and has a large community of users, making it a good intro programming language. Python is also used for &lt;a href="https://www.dataquest.io/blog/how-to-learn-python-for-data-science-in-5-steps/"&gt;data science and artificial intelligence applications&lt;/a&gt;. As such, Python has built in libraries for data science, like Numpy, Pandas, and Tensorflow.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Javascript - 35%
&lt;/h4&gt;

&lt;p&gt;Javascript enables web applications, and is one of the core technologies of the internet/world wide web. Its popularity stemmed from the need to create interactive web pages, and is now a requirement for most web development. Anyone interested in frontend or full-stack development should put a &lt;a href="http://javascript.info/"&gt;focus on JS&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Java - 27%
&lt;/h4&gt;

&lt;p&gt;Java has been declining in popularity since 2016, although it is still one of the most &lt;a href="https://www.businessinsider.com/most-popular-programming-languages-github-2019-11"&gt;popular languages to date&lt;/a&gt;. Its main use is creating enterprise applications, and is also today to create Android apps. Java was designed with the &lt;a href="https://en.wikipedia.org/wiki/Write_once,_run_anywhere"&gt;write once, run anywhere principle&lt;/a&gt;. Java is also taught in many degree computer science programs.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. C++  - 17%
&lt;/h4&gt;

&lt;p&gt;C++ is used today in performant applications, such as games. It is also common in many embedded applications. It's based off of C, but is refactored to be object oriented. It is a high performance language, as it is designed with systems programming in mind.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. SQL - 17%
&lt;/h4&gt;

&lt;p&gt;SQL or Structured Query Language is a domain specific language for querying relational databases. SQL allows you to &lt;a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete"&gt;create, read, update and delete&lt;/a&gt; data tables. Data engineers are typically masters of SQL, while most engineers will have just have some exposure to relational databases.&lt;/p&gt;

&lt;h4&gt;
  
  
  6. Ruby/Ruby on Rails - 16%
&lt;/h4&gt;

&lt;p&gt;Ruby is hardly seen not on rails, and much of the new development for ruby is based on the rails community. Ruby is a similar language to Python, in that it is object oriented, and was designed to be easy to use. Ruby on Rails is used at a lot of established companies for their web frameworks.&lt;/p&gt;

&lt;h4&gt;
  
  
  7. Golang - 13%
&lt;/h4&gt;

&lt;p&gt;Go or Golang is a relatively newer language, created by &lt;a href="https://golang.org/"&gt;top developers at &lt;br&gt;
Google&lt;/a&gt;. Many companies are using Go in their backend, as it is a high performance language. The community is growing for Go, and many companies appear to be looking to start recruiting Go specific engineers.&lt;/p&gt;

&lt;h4&gt;
  
  
  8. CSS - 12%
&lt;/h4&gt;

&lt;p&gt;CSS or Cascading Style Sheets is a stylesheet language for describing the presentation of an HTML document. CSS is another core aspect of web development, and is a must learn for those interested in front end work.&lt;/p&gt;

&lt;h4&gt;
  
  
  9. Typescript - 9%
&lt;/h4&gt;

&lt;p&gt;Typescript is another language growing steadily in popularity - its a refactored version of Javascript that scales better for large applications. It is transpiled to javascript for web development, and has gain traction from those tired of javascript's shortcomings.&lt;/p&gt;

&lt;h4&gt;
  
  
  10. HTML - 9%
&lt;/h4&gt;

&lt;p&gt;HTML or Hypertext Markup Language is the basic building block for web pages. It can be used on its own, or used with CSS and Javascript to make more beautiful and interactive web pages. Similar to Javascript and CSS, HTML is a must learn for web developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;center&gt; Frameworks and other tools:&lt;/center&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. React - 33%
&lt;/h4&gt;

&lt;p&gt;React is a Javascript web framework, which has become the most popular framework due to its simplicity and flexibility.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. AWS - 24%
&lt;/h4&gt;

&lt;p&gt;AWS is Amazon Web Services, which is the largest cloud computing platform.  AWS has many uses, including the basic storage capability.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Node.js - 21%
&lt;/h4&gt;

&lt;p&gt;Node is a runtime environment for executing Javascript outside of the browser. Node.js is very popular for developers since it allows for using javascript everywhere.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Rails - 16%
&lt;/h4&gt;

&lt;p&gt;Rails is a web framework built for Ruby. As mentioned in the Ruby section, it is the main use case today of Ruby. Rails became popular because of its ease of use, and continues to be relevant due to its huge user base and community.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Linux/unix - 13%
&lt;/h4&gt;

&lt;p&gt;Linux is a family of open source operating systems, derived from unix operating systems. Companies looking for linux experience are looking for experience developing and working on the platform.&lt;/p&gt;

&lt;p&gt;I used filters on 930 entry level jobs in San Francisco with the job title “software engineer” or “developer". It should be noted that many job postings were looking for at least one of many languages, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4F-UkHwt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/FgOGSjw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4F-UkHwt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/FgOGSjw.png" alt="An example LinkedIn job posting where multiple languages are acceptable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because this list is using real data from companies that are currently hiring it's a good guide for the current most hirable skills. Other guides have most popular or most used, but that is not representative of the current job market. I hope this helps you in your job search, as I know this information will push me to learn some new things before I start interviewing!&lt;/p&gt;

</description>
      <category>firstyearincode</category>
      <category>career</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
