<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Jeremy Gagon Web Developer</title>
    <description>Blog for full stack web developer Jeremy Gagon.
</description>
    <link>http://jergagon1.github.io/</link>
    <atom:link href="http://jergagon1.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 29 Jun 2015 04:47:56 +0000</pubDate>
    <lastBuildDate>Mon, 29 Jun 2015 04:47:56 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Ruby Method: Roman Numerals</title>
        <description>&lt;p&gt;Code sample of a Ruby method to convert a number into Roman numerals.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/73a13b7729fced86755d.js&quot;&gt; &lt;/script&gt;

</description>
        <pubDate>Sun, 21 Jun 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/06/21/roman-numerals.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/06/21/roman-numerals.html</guid>
        
        <category>ruby method</category>
        
        <category>ruby</category>
        
        <category>code sample</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Ruby Method: Array Selection Sort</title>
        <description>&lt;p&gt;Code sample of Ruby method to do a selection sort of an array.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/f2b4af2241fc8943cc23.js&quot;&gt; &lt;/script&gt;

</description>
        <pubDate>Mon, 15 Jun 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/06/15/array-selection-sort.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/06/15/array-selection-sort.html</guid>
        
        <category>ruby method</category>
        
        <category>ruby</category>
        
        <category>code sample</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Rebasing in Git</title>
        <description>&lt;p&gt;There are powerful commands in Git that give you precise control over your workflow and commit history. One such command is &lt;code&gt;git rebase&lt;/code&gt;. It offers some clear benefits over a standard merge.&lt;/p&gt;

&lt;p&gt;A common workflow in git is to create a branch to develop and test a new application feature. For the sake of this post, let’s say that your main development branch is master. Your feature branch is forked off of master at a particular commit. You then work on the feature branch until you are ready to submit your changes back to master for review and incorporation.&lt;/p&gt;

&lt;p&gt;While you are working on your feature branch, commits might also be occurring on the master branch. These commits move the HEAD of the master branch past the originating commit of your branch. When you merge your changes into master, you potentially introduce merge conflicts as your branch is out-of-date with master.&lt;/p&gt;

&lt;p&gt;Additionally, the commit history becomes jumbled because there are two parallel workflows that are being merged together. The result is a non-linear commit history that is hard to follow.&lt;/p&gt;

&lt;p&gt;Git rebase is a way to address these issues. It basically snips your feature branch off of the master branch from the originating commit and reattaches it to a later commit on master. All of the current changes from master are incorporated into your feature branch commits.&lt;/p&gt;

&lt;p&gt;To use rebase, you checkout your feature branch and type the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git rebase &amp;lt;commit reference&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can provide a commit reference in multiple ways including a *SHA, a branch name or a version tag.&lt;/p&gt;

&lt;p&gt;One side effect of this is that all of the commits on your feature branch will change in terms of the SHA and potentially the code content.&lt;/p&gt;

&lt;p&gt;So what are the potential pitfalls of rebasing?&lt;/p&gt;

&lt;p&gt;When you rebase, you are essentially removing one or more commits from the commit history of the project and replacing them with new commits.&lt;/p&gt;

&lt;p&gt;If someone forked your repo and was working from one of the commits that you removed, you would be creating a complicated upstream problem for them. The parent commit of their fork would be gone.&lt;/p&gt;

&lt;p&gt;For this reason, rebasing in public repos is discouraged and considered bad practice.&lt;/p&gt;

&lt;p&gt;*Interesting side note: The “SHA” mentioned above refers to &lt;a href=&quot;http://en.wikipedia.org/wiki/SHA-1&quot; title=&quot;Link to wikipedia article on SHA-1&quot;&gt;SHA-1&lt;/a&gt; which is a “Secure Hash Algorithm”. It is sometimes used for hashing data in security, but Git uses it to create a unique identifier that guarantees the uniqueness and consistency of the data snapshot in the commit.&lt;/p&gt;
</description>
        <pubDate>Mon, 25 May 2015 21:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/05/25/git-rebase.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/05/25/git-rebase.html</guid>
        
        <category>git</category>
        
        <category>rebase</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Ruby Method: Numbers to Words</title>
        <description>&lt;p&gt;Code sample of a Ruby method to convert a number into English words.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/9d71076154189c723bee.js&quot;&gt; &lt;/script&gt;

</description>
        <pubDate>Wed, 13 May 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/05/13/numbers-to-words.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/05/13/numbers-to-words.html</guid>
        
        <category>ruby method</category>
        
        <category>ruby</category>
        
        <category>code sample</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Active Record: An ORM Framework</title>
        <description>&lt;p&gt;To get an understanding of Active Record, there are a few concepts that must be touched on.&lt;/p&gt;

&lt;p&gt;To have persistant data of any size or complexity in web applications, you need to store your data in a database. There are different database structures, but a common one is a relational database.&lt;/p&gt;

&lt;p&gt;A relational database is used to store information in tables where a column represents an attribute of data and a row represents an individual data record. For example, if a web application needs to store a person’s address, there would be an address column in a customer table and the specific customer would be represented by a row.&lt;/p&gt;

&lt;p&gt;Web application frameworks are systems that allow servers to be programmed using an object-oriented language like Ruby.&lt;/p&gt;

&lt;p&gt;In the model-view-controller (MVC) web application framework Ruby on Rails, the “model” is responsible for rules, logic and data management. Active Record is the “model” in that relationship.&lt;/p&gt;

&lt;p&gt;More specifically, Active Record is the mechanism of linking an object-oriented class to a relational database table. The class wraps the database table so that when a class instance is created, a linked row is created in the database table. Due to this relationship, it is known as a framework for Object Relational Mapping (ORM).&lt;/p&gt;

&lt;p&gt;Active record allows you to use the features of class-based object-oriented programming to streamline database information management. You can use OOP features like inheritance to create logical hierarchies in your database.&lt;/p&gt;

&lt;p&gt;Also, you can use the object-oriented language to create and update data to minimize SQL programming.&lt;/p&gt;
</description>
        <pubDate>Sun, 25 Jan 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/01/25/active-record.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/01/25/active-record.html</guid>
        
        <category>orm</category>
        
        <category>active record</category>
        
        <category>ruby</category>
        
        <category>mvc</category>
        
        <category>model</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Ruby Hashes vs JavaScript Objects</title>
        <description>&lt;p&gt;When you frequently work several different coding languages, you begin to notice interesting comparisons between the languages.&lt;/p&gt;

&lt;p&gt;One interesting comparison can be drawn between a Ruby hash and a JavaScript object. Let’s take a look at the examples of each:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Ruby hash literal&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:occupation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Web Developer&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Ruby hash literal with alternate syntax&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;occupation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Web Developer&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// JavaScript object&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;occupation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Web Developer&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, using the alternate Ruby hash literal syntax is essentially the same as using the literal object constructor in JavaScript. They both commonly use symbols for the name or key and can contain any type of object as the value.&lt;/p&gt;

&lt;p&gt;They look the same syntactically, so what’s the difference?&lt;/p&gt;

&lt;p&gt;A Ruby hash is itself an object, in fact, it’s an instance of the Hash class. It inherits attributes and methods from the Object parent class and has additional built-in functionality from the Enumerable module.&lt;/p&gt;

&lt;p&gt;A hash exists to deal with collections of data. It is an object that’s a container for other objects. It allows you to assign a name to each separate value in your collection.&lt;/p&gt;

&lt;p&gt;In contrast, a JavaScript object is a complex data structure that allows you to store a collection of properties and methods. These properties and methods define the state and abilities of the object.&lt;/p&gt;

&lt;p&gt;Using the literal object constructor, you are creating a one-off object.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myNewObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;property1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;property2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here I have created a new object myNewObject that has properties and values. If I wanted to create another similar object, I would need to repeat the code with a different variable name.&lt;/p&gt;

&lt;p&gt;In a way, this could be considered similarity between a JavaScript object created in this way and a Ruby hash literal instance. Neither the literal object constructor in JavaScript or an instance of the Hash class in Ruby is designed to  make multiple instances of itself. It is designed to be a unique object instance containing other objects.&lt;/p&gt;

&lt;p&gt;There is an alternate form of object construction in JavaScript where you can create multiple similar objects through a constructor function, but the syntax is different to Ruby hashes and not specifically related to the issue here.&lt;/p&gt;

&lt;p&gt;There are some similarities between a Ruby hash and a JavaScript literal object that extend beyond appearance alone. However, the use and implementation can be different in the two languages.&lt;/p&gt;
</description>
        <pubDate>Sun, 18 Jan 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/01/18/ruby-hashes-vs-js-objects.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/01/18/ruby-hashes-vs-js-objects.html</guid>
        
        <category>ruby</category>
        
        <category>hash</category>
        
        <category>javascript</category>
        
        <category>object</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Classes vs Modules in Ruby</title>
        <description>&lt;p&gt;Classes and Modules are both ways to organize code in Ruby. They share some common structural elements but are fundamentally different in nature and work together to build powerful objects.&lt;/p&gt;

&lt;p&gt;A class is a construct to provide a blueprint for an object. Once you have defined a class with it’s attributes and methods you can create an instance of the class. The class instance - or object - will have all of the capabilities of the class.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@sex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sex&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;stand&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# stand up&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walk&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# move one foot in front of other&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;talk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;span class=&quot;n&quot;&gt;john_smith&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;John Smith&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;male&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;john_smith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stand&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# John stands up&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;john_smith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;talk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Hi, I&amp;#39;m a person&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Hi, I&amp;#39;m a person&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;A class can inherit behavior from a single parent class.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Male&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;bill_smith&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Male&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bill_smith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;walk&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# I inherited this from my parent class&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Modules, by comparison, have two primary functions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Modules allow you to define functionality outside of a class and mix it in to one or more classes. You can add multiple modules to simulate multiple inheritance which Ruby does not allow.&lt;/li&gt;
  &lt;li&gt;Modules allow you to create a namespace to avoid clashes of similarly named classes, methods and constants.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s assume we want to include some functionality in our person class that is common to all mammals. We don’t want to chain a series of classes all the way from a person to a mammal in the animal kingdom, but it would be helpful to have some mammal functionality.&lt;/p&gt;

&lt;p&gt;We could define a mammal module and mix it in to our class using the &lt;q&gt;include&lt;/q&gt; keyword.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Mammal&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;grow_hair&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# hair grow&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;breathe_air&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# breathe&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;

  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Mammal&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# rest of code here...&lt;/span&gt;

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

&lt;span class=&quot;n&quot;&gt;jane_smith&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;jane_smith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;breathe_air&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The namespace functionality allows you to wrap a class or method in a module so that it can be referred to using the module modifier to avoid any naming clashes.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Athlete&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# running fast here&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CouchPotato&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# not running so fast here&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;athlete&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Athlete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;athlete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Wow, that was fast!&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;jeremy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CouchPotato&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;jeremy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Not so much...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
        <pubDate>Sun, 11 Jan 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/01/11/classes-vs-modules-ruby.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/01/11/classes-vs-modules-ruby.html</guid>
        
        <category>ruby</category>
        
        <category>class</category>
        
        <category>module</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Ruby Classes</title>
        <description>&lt;p&gt;When you develop with an object-oriented programming language like Ruby, you work with objects. These objects can be representations of things in the real world, for example you could have a person object or a car object. However, a programming object does not have to represent a material physical thing. A programming object can also represent an abstract concept like an idea, a number, a conversation or a piece of data.&lt;/p&gt;

&lt;p&gt;Objects have attributes that describe their state. A car object might have a color attribute that would describe its color or a number_of_doors attribute to describe if it’s a 2 door or 4 door.&lt;/p&gt;

&lt;p&gt;Just like in the real world, objects in programming are typically able to do things. Our car object would be able to drive, brake, turn, etc…&lt;/p&gt;

&lt;p&gt;In Ruby, &lt;q&gt;doing things&lt;/q&gt; is accomplished by methods. A method is like a function or algorithm that gives an object a set of instructions on how to accomplish a task. You ask the object to do the task by calling the method of the object.&lt;/p&gt;

&lt;p&gt;In Ruby - and many other programming languages - objects are constructed using classes. A class is simply a blueprint for how to build an object. You define the attributes and methods for the object in the class. Once you have defined the attributes and methods, you can create or &lt;q&gt;instantiate&lt;/q&gt; an instance of the class to use in your program. The class instance will have all of the capabilities defined in the class.&lt;/p&gt;

&lt;p&gt;Each instance of a class might have attributes that vary from instance to instance. For example, not all cars are red, so you would want to be able to instantiate a car object that could be a different color. You could build a separate class for red cars, blue cars, yellow cars, etc… but it would not be very efficient: &lt;code&gt;duplicate_code == bad? true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Luckily, there are instance variables in Ruby classes that allow you to set the state of a particular attribute in a class instance. They do not affect the other objects created from the class.&lt;/p&gt;

&lt;p&gt;Let’s see how it works.&lt;/p&gt;

&lt;p&gt;We can build a Ruby class using the class definition keyword:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# code goes here&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That was easy. Now we can create an instance of the class using the new method of the car class and save it to a variable my_car.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Great, we have a new car object, but we haven’t defined anything about the state of the car. To solve that, we can set up  instance variables in the class definition. They are prefaced with a @ symbol and they have scope anywhere within the class definition.&lt;/p&gt;

&lt;p&gt;We can use a special initialize method that can accept arguments at the time of instantiation. These arguments are then assigned to instance variables within the initialize method.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@make&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# assign the argument make to the instance variable @make&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@year&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;year&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Honda&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Fit Sport&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2010&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;orange&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we can create a car object from the car class and set its instance variables. Unfortunately, we still can’t access the variables to read or change them once they have been set up.&lt;/p&gt;

&lt;p&gt;We need reader and writer methods to accomplish this (these are similar to getter and setter methods in other languages).&lt;/p&gt;

&lt;p&gt;In our class definition, we could add a reader method like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;make&lt;/span&gt;
  &lt;span class=&quot;vi&quot;&gt;@make&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or a writer method:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# The equals sign is part of the method name.&lt;/span&gt;
  &lt;span class=&quot;vi&quot;&gt;@color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These allow you to access the instance variable using a dot notation on your class instance.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Honda&amp;quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# This format also works thanks to syntactic sugar&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;I just painted my car!&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ruby has a nice shortcut for setting up reader and writer methods. The shortcut uses the attr_reader, attr_writer or attr_accessor methods. The attr_reader is a reader method, attr_writer is a writer method and attr_accessor will both read and write to a particular instance variable.&lt;/p&gt;

&lt;p&gt;The examples above could be rewritten:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;kp&quot;&gt;attr_reader&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:make&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;attr_writer&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:color&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:color&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# To read and write&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So far, we’ve set up some attributes for our car class and saved them in instance variables and we can access the instance variables using reader and writer methods.&lt;/p&gt;

&lt;p&gt;Our car still can’t really do anything. To fix that, we need to add some instance methods.&lt;/p&gt;

&lt;p&gt;Instance methods are methods defined within the class definition that can be called on an instance of a class.&lt;/p&gt;

&lt;p&gt;We can create a start_engine method that will start the car. It can interact with a boolean instance variable &lt;code&gt;@ignition_on&lt;/code&gt; so we don’t damage the starter.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;start_engine&lt;/span&gt;
  &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can also add methods that accept arguments to give the car object more flexibility. Here’s a drive method that can accept a direction argument so you could use forward, backward, left, right, North, South, etc…&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;vi&quot;&gt;@moving&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;We are driving &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;forward&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;We are driving forward.&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using classes is a great way to create smart usable objects that can contain attributes and execute actions in Ruby. For fun, the code below shows a more flushed out car class. Until next time…&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_capacity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_measurement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@make&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@year&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;year&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fuel_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_type&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fuel_capacity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_capacity&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fuel_measurement&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuel_measurement&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fuel_level_percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@moving&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Reader method to check the fuel gauge&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;attr_reader&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:fuel_level_percent&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;start_engine&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The car won&amp;#39;t start if you&amp;#39;re out of gas&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@fuel_level_percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Start the ignition if it is not already on&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@iginition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Time to fuel up!&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fuel_up&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Turn off the engine before filling up&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@ignition_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Determine percent of tank empty and multiple by fuel capacity to know how much fuel to buy&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fuel_needed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@fuel_level_percent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@fuel_capacity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_i&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;You need &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fuel_needed&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@fuel_measurement&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; of &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@fuel_type&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Fill up the tank&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fuel_level_percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# move the car object&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@moving&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;We are driving &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;brake&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@moving&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@moving&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

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

&lt;span class=&quot;n&quot;&gt;my_car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Honda&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Fit Sport&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2010&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;orange&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;gas&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;gallons&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 02 Jan 2015 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2015/01/02/ruby-classes.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2015/01/02/ruby-classes.html</guid>
        
        <category>ruby</category>
        
        <category>class</category>
        
        <category>object</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>The group_by Method in Ruby</title>
        <description>&lt;p&gt;Welcome back to my blog!&lt;/p&gt;

&lt;p&gt;This week I am looking at the Ruby &lt;code&gt;group_by&lt;/code&gt; method of the Enumerable module.&lt;/p&gt;

&lt;p&gt;In case you don’t know what an Enumerable module is:&lt;/p&gt;

&lt;p&gt;In Ruby, a class definition is where we provide the blueprint for an object. We then create an object from that class by instantiating a new instance of the class.&lt;/p&gt;

&lt;p&gt;In our class definition, we teach our object how to do stuff by defining methods that we can then ask our object to do.&lt;/p&gt;

&lt;p&gt;Sometimes dissimilar classes of objects have similar methods: you could have a &lt;code&gt;set_sale_price&lt;/code&gt; method for a car object and a frying pan object. These objects would not come from the same inherited class, but could benefit from use of the same method. That’s where modules come in.&lt;/p&gt;

&lt;p&gt;A module provides a set of common methods or functions that can be added or &lt;q&gt;Mixed in&lt;/q&gt; to a class definition. The objects instantiated from that class can then use those methods.&lt;/p&gt;

&lt;p&gt;The Enumerable module provides a set of methods to deal with collections of data like arrays and hashes. Using Enumerable methods, you can iterate through a collection of data and do things with some or all of the elements of that data.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;group_by&lt;/code&gt; method is one of the perhaps lesser-known Enumerable methods. However, it does interesting things and can be very useful in the right context.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;group_by&lt;/code&gt; method allows you to iterate through a set of data and group the contents of the data according to a block of code that you pass in.&lt;/p&gt;

&lt;p&gt;The returned object is a hash where each hash key represents a category of the sorted data. The hash value is a nested array containing all of the elements from the original array or hash that meet the criteria.&lt;/p&gt;

&lt;p&gt;Let’s look at an array example: I will take an array of numbers and organize them into groups of those that are evenly divisible by 2 and those that are not.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# my initial array&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_number_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# call group_by method to find numbers evenly divisible by 2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_number_array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group_by&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# returned hash sorting data into groups&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, the keys are boolean categories of evenly divisible by 2 (true) and not evenly divisible by two (false).&lt;/p&gt;

&lt;p&gt;It’s actually really handy! If you capture the returned hash in a variable, you have access to the sorted data set for future processing.&lt;/p&gt;

&lt;p&gt;What happens when you call group_by on a hash?&lt;/p&gt;

&lt;p&gt;When you call group_by on a hash, you are also returned a new hash. The hash contains keys that represent the categories. Like arrays, the value for each key is a nested array. Due to the structure of hashes, the nested arrays that serve as the hash values contain other nested arrays for each key-value pair from the original hash that satisfy the criteria.&lt;/p&gt;

&lt;p&gt;Here’s a hash example. Let’s figure out which values are strings and which are not:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:first_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:last_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_hash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group_by&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_a?&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:first_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:last_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Like with all Enumerable methods, as you use it more, you understand the context in which it is most effective. I will definitely keep this method in mind as I’m tackling new problems with Ruby data collections.&lt;/p&gt;
</description>
        <pubDate>Sun, 14 Dec 2014 17:00:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2014/12/14/groupby-method.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2014/12/14/groupby-method.html</guid>
        
        <category>array</category>
        
        <category>hash</category>
        
        <category>enumerable</category>
        
        <category>group_by</category>
        
        
        <category>technical</category>
        
      </item>
    
      <item>
        <title>Arrays and Hashes in Ruby</title>
        <description>&lt;p&gt;In this week’s tech blog, I will dive into arrays and hashes in the Ruby programming language. What are they? How do they work? Why would I use them? Do they have anything to do with corned beef? Spoiler alert: the answer to the last one is NO!&lt;/p&gt;

&lt;p&gt;I could start off with some dry technical definitions about what they are (an integer-indexed list of…), but I find that these things make more sense when you identify the problem they are solving. Most things in computer programming are created to solve a problem whether it’s making something faster or easier, sorting complex sets of data, communicating information or whatever.&lt;/p&gt;

&lt;p&gt;Here’s a simple scenario: I want to build an application where I can store the top 100 current pop singles. My app needs to store the following information:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Song Ranking/Position (This part’s important, right? It tells which artist has the #1 single)&lt;/li&gt;
  &lt;li&gt;Song Title&lt;/li&gt;
  &lt;li&gt;Artist&lt;/li&gt;
  &lt;li&gt;Album&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the features that I need in my app:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The ability to move songs up and down the list week-to-week as the songs become more or less popular.&lt;/li&gt;
  &lt;li&gt;Keep the song title, artist and album all together as the songs are moved up and down the list.&lt;/li&gt;
  &lt;li&gt;The ability to query my app to find things like &lt;q&gt;Which artist has the #10 single&lt;/q&gt; or &lt;q&gt;What are the top 10 songs?&lt;/q&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, so where to start?&lt;/p&gt;

&lt;p&gt;In computer programming, you can store information in variables. Variables point to little pieces of computer memory. The information stored in that location can change (hence the name &lt;q&gt;variable&lt;/q&gt;), but essentially you are giving a name to a memory location: the thing that I’m calling &lt;code&gt;my_variable&lt;/code&gt; occurs here. Variables are, borrowing the words of George Carlin, &lt;q&gt;A place for my stuff&lt;/q&gt;.&lt;/p&gt;

&lt;p&gt;If I put all of the songs, artists and albums in separate variables, they won’t really have a relationship to one another. How would I know that &lt;q&gt;Don’t&lt;/q&gt; is by Ed Sheeran and that it’s on his album &lt;q&gt;X&lt;/q&gt;? How would I sort all of these songs by popularity?&lt;/p&gt;

&lt;p&gt;So, we have identified two problems to solve:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We need to keep the song name, artist and album together and be able to reference which piece of information is which, like &lt;q&gt;Maroon 5&lt;/q&gt; is the artist and &lt;q&gt;Animals&lt;/q&gt; is the song. There could be (and was) an artist &lt;q&gt;The Animals&lt;/q&gt; so we need to make sure we can tell the difference.&lt;/li&gt;
  &lt;li&gt;We need to be able to take the groups of values that we created in step 1 and put them in a numerical order based on popularity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;problem-1&quot;&gt;Problem 1&lt;/h3&gt;

&lt;p&gt;We need a data structure that can store a collection of values (&lt;q&gt;Blank Space&lt;/q&gt;, Taylor Swift, 1989) and reference those values with a name or &lt;q&gt;key&lt;/q&gt; for each value (song_name, artist, album). Luckily, someone encountered this problem before in ruby programming and created the data structure &lt;code&gt;hash&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;solution-the-hash&quot;&gt;Solution: The Hash&lt;/h3&gt;

&lt;p&gt;A hash in Ruby is a collection of key, value pairs. You could write the syntax:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;song_1_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;song_name&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Blank Space&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;artist&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Taylor Swift&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;album&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;1989&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first thing in quotes is the key and it points to (=&amp;gt;) the second thing in quotes which is the value. Each different key value pair is separated by a comma. The quotes just mean the things are strings of text. You can put things other than strings in hashes (like numbers) and in those cases you would omit the quotes. The quotes are only for strings.&lt;/p&gt;

&lt;p&gt;The key in Ruby hashes is often just a symbolic name for the value it is pointing to, so you can use a different syntax which does not take up all the memory of storing the separate keys as text strings. You can use a symbol:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;song_1_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:song_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Blank Space&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Taylor Swift&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;1989&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s easy - you just leave off the quotes for the key and put a colon before it. Now I can reference my song name like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;song_1_hash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:song_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I would get back:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Blank Space&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Okay, so by using hashes we have a solution to the first problem. We can keep all of the information about a particular song in one object and individually reference the song name, artist and album. What about the second problem?&lt;/p&gt;

&lt;h3 id=&quot;problem-2&quot;&gt;Problem 2&lt;/h3&gt;

&lt;p&gt;Now we need to sort each song numerically. We want to know which song is #1, 2, 3, 4…. The order is important. Luckily, this problem has also been addressed previously in the ruby language!&lt;/p&gt;

&lt;h3 id=&quot;solution-the-array&quot;&gt;Solution: The Array&lt;/h3&gt;

&lt;p&gt;An array is a collection of values where each value has an associated number or &lt;q&gt;index&lt;/q&gt;. The index identifies the position of the value in the list. Let’s look at an example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;first thing&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;second thing&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;third thing&amp;quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, to get the first item in the array:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;#returns the value below&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;second thing&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Wait, what just happened? There’s a catch: array indexes start with 0 instead of 1.&lt;/p&gt;

&lt;p&gt;To get the first item using the correct syntax:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;#returns the value below&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;first thing&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Okay, so how do we use our song hashes and an array to create the top 100 list?&lt;/p&gt;

&lt;p&gt;An array can contain any other type of Ruby object: strings, numbers, other arrays and even hashes.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;my_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;string here&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;nested&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;array&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;here&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:first_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Jeremy&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:last_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Gagon&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, to start building my app, I would create an empty array top_100_songs:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I could then assign a hash to each postion of the array based on it’s current ranking:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Animals&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Maroon 5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Animals&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Don&amp;#39;t&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Ed Sheeran&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;X&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# etc...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;so now my array is:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Animals&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Maroon 5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Animals&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Don&amp;#39;t&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Ed Sheeran&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;X&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# etc...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I can find out the number one hit song by typing:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# returns the value below&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;Animals&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are ruby algorithms (or methods) associated with the array object that will allow me to sort, insert, move or remove the songs in the array as songs lose or gain popularity. For example, the code below adds a new song to the beginning of the top 100 list and shift all the previous values by 1 place to the right.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;top_100_songs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unshift&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:song_title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Black Widow&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:artist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Iggy Azalea featuring Rita Ora&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:album&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;The New Classic&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’ve created our app and solved our problems using the ruby hash and array objects. To recap, a hash is a collection of key value pairs and an array is a collection of items where each item has a numerical index (starting with 0!!!).&lt;/p&gt;

&lt;p&gt;Until next time.&lt;/p&gt;
</description>
        <pubDate>Sun, 07 Dec 2014 15:30:00 +0000</pubDate>
        <link>http://jergagon1.github.io/technical/2014/12/07/arrays-hashes-ruby.html</link>
        <guid isPermaLink="true">http://jergagon1.github.io/technical/2014/12/07/arrays-hashes-ruby.html</guid>
        
        <category>array</category>
        
        <category>hash</category>
        
        
        <category>technical</category>
        
      </item>
    
  </channel>
</rss>
