<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rob Fairchild on Medium]]></title>
        <description><![CDATA[Stories by Rob Fairchild on Medium]]></description>
        <link>https://medium.com/@robertjoseph.fairchild?source=rss-c84912fa27c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NPWxXgcljcG0KCaIAJhiZQ.jpeg</url>
            <title>Stories by Rob Fairchild on Medium</title>
            <link>https://medium.com/@robertjoseph.fairchild?source=rss-c84912fa27c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 08:19:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@robertjoseph.fairchild/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ruby Shortcuts: Pulling info from Hashes (.values & .keys)]]></title>
            <link>https://codeburst.io/ruby-shortcuts-pulling-info-from-hashes-values-keys-1d6ba0256b2d?source=rss-c84912fa27c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d6ba0256b2d</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[hash]]></category>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Rob Fairchild]]></dc:creator>
            <pubDate>Fri, 17 Nov 2017 03:23:40 GMT</pubDate>
            <atom:updated>2021-01-27T06:25:43.742Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AdK1aHiQ2u59GMAPODQiuA.png" /></figure><p>Here I am again, sitting at my computer tearing my hair out trying to pull individual values out of hashes for yet another project. Why isn’t there an easier way than to individually identify each piece of code you need in a MONSTER sized hash besides calling each piece out by name?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*lVpvLnCZ7ySi2oq6Z7C7iw.gif" /></figure><p>Ohh wait, there is! Let me show you an example of the long (more difficult) way of doing this.</p><p>Lets say you have an array of hashes as follows:</p><pre>furniture = [<br>  {&quot;name&quot; =&gt; &quot;bed&quot;, &quot;color&quot; =&gt; &quot;black&quot;, &quot;size&quot; =&gt; &quot;12&quot;, &quot;date_created&quot; =&gt; &quot;12/6&quot;, &quot;condition&quot; =&gt; &quot;New&quot;},<br>  {&quot;name&quot; =&gt; &quot;chair&quot;, &quot;color&quot; =&gt; &quot;brown&quot;, &quot;size&quot; =&gt; &quot;7&quot;, &quot;date_created&quot; =&gt; &quot;6/13&quot;, &quot;condition&quot; =&gt; &quot;Used&quot;},<br>  {&quot;name&quot; =&gt; &quot;desk&quot;, &quot;color&quot; =&gt; &quot;maple&quot;, &quot;size&quot; =&gt; &quot;9&quot;, &quot;date_created&quot; =&gt; &quot;2/13&quot;, &quot;condition&quot; =&gt; &quot;New&quot;}<br>]</pre><p>This is what most beginning coders would initially want to do to pull the values from the above array of hashes:</p><pre>furniture_values = (furniture.map {|furn| [furn[&quot;name&quot;], furn[&quot;color&quot;], furn[&quot;size&quot;], furn[&quot;date_created&quot;], furn[&quot;condition&quot;]]})</pre><pre>p furniture_values</pre><p>This will print:</p><pre>[[&quot;bed&quot;, &quot;black&quot;, &quot;12&quot;, &quot;12/6&quot;, &quot;New&quot;], [&quot;chair&quot;, &quot;brown&quot;, &quot;7&quot;, &quot;6/13&quot;, &quot;Used&quot;], [&quot;desk&quot;, &quot;maple&quot;, &quot;9&quot;, &quot;2/13&quot;, &quot;New&quot;]]</pre><p>This is the right answer, you got all the values you needed but individually going through and spouting off all of the key names to get the right info is tedious! There is a much better way. Let me show you how I would achieve the same result in the code below!</p><pre>p furniture.map {|x| x.values}</pre><p>This will also print:</p><pre>[[&quot;bed&quot;, &quot;black&quot;, &quot;12&quot;, &quot;12/6&quot;, &quot;New&quot;], [&quot;chair&quot;, &quot;brown&quot;, &quot;7&quot;, &quot;6/13&quot;, &quot;Used&quot;], [&quot;desk&quot;, &quot;maple&quot;, &quot;9&quot;, &quot;2/13&quot;, &quot;New&quot;]]</pre><p>Umm… need I say more? How much easier is that? The .values method will simply pull all of the values out of your hash and display them nicely for you. You get the same result as before but is much quicker and easier. If for whatever reason you wanted to pull the “keys” out of your hash, you would simply write the following code:</p><pre>p furniture.map {|x| x.keys}.uniq</pre><p>( .uniq is a function that erases all duplicate keys or values)</p><p>Boom, this will print your keys:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/306/1*O094GNtGMjcg2-gbAtZTFA.gif" /></figure><pre>[&quot;name&quot;, &quot;color&quot;, &quot;size&quot;, &quot;date_created&quot;, &quot;condition&quot;]</pre><p>Maybe you just wanted to pull the names of each piece of furniture? Just the colors? Simply just tag the ID assigned to the key you wish to print. Here’s the code to pull just the names:</p><pre>p furniture.map {|x| x.values[0]}</pre><p>This will print:</p><pre>[&quot;bed&quot;, &quot;chair&quot;, &quot;desk&quot;]</pre><p>Simple as that. Remember these shortcuts! When writing deep complex code you may need to pull the values or keys out of your hash and use it somewhere else. Using this strategy is a MUCH easier way of doing so. Questions or comments? Feel free to add them below. Happy Coding, see you next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d6ba0256b2d" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/ruby-shortcuts-pulling-info-from-hashes-values-keys-1d6ba0256b2d">Ruby Shortcuts: Pulling info from Hashes (.values &amp; .keys)</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comparing Ruby Loop Methods]]></title>
            <link>https://codeburst.io/comparing-ruby-loop-methods-62d39b8260c2?source=rss-c84912fa27c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/62d39b8260c2</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[while-loop]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[times-loop]]></category>
            <category><![CDATA[each-loop]]></category>
            <dc:creator><![CDATA[Rob Fairchild]]></dc:creator>
            <pubDate>Sun, 12 Nov 2017 20:02:17 GMT</pubDate>
            <atom:updated>2017-11-14T13:47:28.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tt6qQGBhqPpT9n0Hw6-cEg.jpeg" /></figure><h3>Introduction</h3><p>During my early days of coding I was introduced to many different methods of achieving the same goal. At first I was ridiculously confused! When to use what method for what situation was driving me crazy. Today I am going to show you the 3 “loop” methods I currently use in the Ruby programming language: the “times” loop, the “each” loop, and the “while” loop.</p><p>I wont go over every single detail of each loop and when it should be used but I will show you a strategy to help you understand how you can use each method to get to the same result. My strategy is to take an exercise that requires a loop to complete, and then complete it using each loop method separately. This will allow you to compare and contrast the methods so that you understand how each one is working. Once you have done this, you can then save it as a reference so that if you ever run into a situation where you would like to transfer the same code to a different loop method, you can just refer to your example.</p><h3>Example:</h3><p>My instructor wanted us to take an array of numbers and find the average of those numbers using a “while” loop. I took this opportunity to to write the code for this exercise using all 3 methods to gain a better grasp on the overall goal. I started with a basic “times” loop. As my instructor always tells me, “If you don’t understand how to do a problem with a times loop, you can’t fully grasp how to do it with the rest of the loops.</p><p>Find the average of the following array of numbers using loops.</p><p>[2, 1, 7, 5]</p><h4>Times Loop method :</h4><pre>def average(input_num_nums)     </pre><pre>sum = 0 <br>index = 0</pre><pre>input_num_nums.length.times do <br>    number = input_num_nums[index]<br>    sum += number <br>    index += 1</pre><pre>end <br>  sum.to_f / input_num_nums.length</pre><pre>end</pre><pre>p average([2, 1, 7, 5])</pre><h3>Each Loop method :</h3><pre>def average(input_num_nums)       <br>  <br>  sum = 0 <br>  <br>  input_num_nums.each do | number | <br>    sum += number</pre><pre>end</pre><pre>sum / input_num_nums.length.to_f</pre><pre>end</pre><pre>p average([2, 1, 7 ,5])</pre><h3>While Loop method:</h3><pre>def average(input_num_nums)</pre><pre>sum = 0<br>index = 0</pre><pre>while index &lt; input_num_nums.length <br>    sum += input_num_nums[index]<br>    index += 1 <br>  end</pre><pre>sum.to_f / input_num_nums.length</pre><pre>end</pre><pre>p average([2, 1, 7, 5])</pre><p>Your output to all 3 methods should be 3.75!</p><h3>Conclusion:</h3><p>There you have it. Now that you have 3 different ways to solve the same problem, you can always look back to see how you can do a similar problem with a different method. From this point on I would recommend doing a few different problems involving loops and write them out with 3 different methods. This will not only help you understand how loops are related but you now have a reference for any future exercises or problems you may encounter! If you have any questions or comments about this post don’t hesitate to chime in! Clap it up if you like!</p><p>See you next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62d39b8260c2" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/comparing-ruby-loop-methods-62d39b8260c2">Comparing Ruby Loop Methods</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>