<?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 Scott Gale on Medium]]></title>
        <description><![CDATA[Stories by Scott Gale on Medium]]></description>
        <link>https://medium.com/@galejscott?source=rss-bd22d070a3f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Zfszu4eojz3_UjuMnfV5Pg.jpeg</url>
            <title>Stories by Scott Gale on Medium</title>
            <link>https://medium.com/@galejscott?source=rss-bd22d070a3f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 01:16:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@galejscott/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[Javascript(Asynchronicty) {]]></title>
            <link>https://medium.com/@galejscott/javascript-asynchronicty-e5e65af45040?source=rss-bd22d070a3f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5e65af45040</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Scott Gale]]></dc:creator>
            <pubDate>Wed, 31 Jul 2019 14:36:46 GMT</pubDate>
            <atom:updated>2019-07-31T16:20:22.344Z</atom:updated>
            <content:encoded><![CDATA[<h4>when communicating with the server};</h4><p>Good morning, we’ve made the jump to javascript, and coming from the happiness of ruby. Javascript sadly, doesn’t have the same loving touch; but she is a lot more exciting, (once she’s up and running).</p><p>A key difference with javascript is asynchronicity. User interfaces are asynchronous by nature, and spend most of their time waiting for user input to interrupt the event loop and trigger event handlers. This is important in JavaScript, because it is a very natural fit for user interface code, and very beneficial to performance on the server.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3oriO7A7bt1wsEP4cw%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3oriO7A7bt1wsEP4cw%2Fsource.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3oriO7A7bt1wsEP4cw%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="304" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cecccd00225b17ce419b8cb6b8a157a/href">https://medium.com/media/7cecccd00225b17ce419b8cb6b8a157a/href</a></iframe><p>Browsers have to manage a lot. They’re animating a gif, they&#39;re displaying text, they&#39;re listening for clicks and scrolls and running JavaScript programs. To do all that work efficiently, browsers use an <em>asynchronous</em> execution model. That’s a fancy way of saying “they do little bits of lots of tasks until the tasks are done.”</p><h3>Using a Metaphor for Synchronous Versus Asynchronous Work</h3><p>A easy way to explain is through a metaphor; albeit a slightly disgusting metaphor; but it’s how I was taught and helped it stick in my memory! Let’s imagine Sam the chef in a kitchen preparing a big meal. He’s only one chef in the kitchen. Sam could prepare a boiled goose, then prepare some potatoes, then prepare some biscuits, then prepare a salad, and then serve it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*8X_UdU55YpSkNgVvUuDgew.gif" /></figure><p>Sam’s diners would be treated to cold boiled goose, hardened potatoes, cold biscuits, and a oh so fresh salad! This is not the goal. This meal was prepared in a <em>synchronous</em> model: one-thing-after-the-other. The boiling of the goose “blocked” the rest of things that were waiting for work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/0*dfuUHfVOptOaFFx-.gif" /></figure><p><em>Instead</em>, Sam the chef should move between each of these tasks quickly. Sam should use the <em>asynchronous</em> execution model browsers use. They should stuff the goose, they should measure the ingredients for the biscuits, they should peel the potatoes, etc. in a loop, <em>as fast as possible</em> so that all the tasks <em>seem</em> to be advancing at the same time. If Sam were to adopt this <em>asynchronous</em> model of work, the diners would be treated to piping-hot boiled goose, steaming potatoes, soft biscuits, and a fresh salad.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*HYTLwPIh-_ptBfF4.gif" /></figure><h3>Describing a Synchronous Code Bloc</h3><pre><strong>let</strong> sum <strong>=</strong> 1 <strong>+</strong> 1; <em>// Line 1<br></em><strong>let</strong> list <strong>=</strong> document.querySelectorAll(“li”); <em>// Line 2</em></pre><p>In this case, when we hit the definition of sum, this work doesn&#39;t rely on any &quot;questionable&quot; or &quot;unknowably long&quot; process. As soon as the work of Line 1 is done, JavaScript will then go to work finding elements and assigning them to list in Line 2.</p><p>But let’s consider a “blocking” operation. Imagine we had a synchronous function called synchronousFetch(&quot;URL STRING&quot;)that fetches data from the network.</p><pre><strong>let</strong> tooMuchData <strong>=</strong> synchronousFetch(“http://genome.example.com/...&quot;); <em>// Line 1<br></em><strong>let</strong> list <strong>=</strong> document.querySelectorAll(“li”); <em>// Line 2<br></em>console.log(tooMuchData);</pre><p>That work in Line 1 could take a long time (e.g. slow network), or might fail (e.g. failed login), or might retrieve a <strong><em>huge</em></strong> amount of data (e.g. The Human Genome).</p><p>It’s possible that the let list in Line 2 <em>will never execute</em>! While JavaScript is executing synchronousFetch it will not be able to animate gifs, you won&#39;t be able to open a new tab, it will appear &quot;locked up.&quot; Recall our Sam the chef metaphor: while Sam prepares the biscuits, the mashed potatoes grow cold and the boiled goose congeals. Gross.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FpVAMI8QYM42n6%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FpVAMI8QYM42n6%2Fsource.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FpVAMI8QYM42n6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/65700e25fb9dea411556ee43c09aee16/href">https://medium.com/media/65700e25fb9dea411556ee43c09aee16/href</a></iframe><h3>Describe an Asynchronous Code Bloc</h3><p>Asynchronous code in JavaScript looks a lot like event handlers. And if we think about it, that makes sense. You tell JavaScript:</p><blockquote><em>Hey, do this thing. </em>And then<em> go do whatever maintenance you need: animate that gif, whatever. But when that first thing has an “I’m done” event, go </em><strong><em>back</em></strong><em> to it and do some work that I defined in a function when I called it.</em></blockquote><p>Let’s imagine a function called asynchronousFetch that takes as arguments:</p><ol><li>A URL String</li><li>An arrow function that will have the fetched data passed into it as its first argument when the asynchronousFetch work is done</li></ol><pre>asynchronousFetch(“http://genome.example.com/...&quot;, tonOfGeneticData <strong>=&gt;</strong> sequenceClone(tonOfGeneticData)); <em>// Line 1</em></pre><pre><strong>let</strong> list <strong>=</strong> document.querySelectorAll(“li”); <em>// Line 2</em></pre><p>In this case, JavaScript <em>starts</em> the asynchronousFetch in Line 1, and then sets list in Line 2. Some time later (who knows how long?), the fetch of data finishes and <em>that</em> data is passed into the &quot;callback&quot; function as tonOfGeneticData — back on Line 1.</p><p>Most asynchronous functions in JavaScript have this quality of “being passed a callback function.” It’s a helpful tool for spotting asynchronous code “in the wild.”</p><h3>Identifying a Synchronous Code Bloc</h3><p>As we have experienced in JavaScript, our code executes top-to-bottom, left-to-right.</p><pre><strong>function</strong> getData(){<br>console.log(“2. Returning instantly available data.”);<br><strong>return</strong> [{name: “Lloyd Christmas”}, {name: “Harry Dunne”}]<br>}</pre><pre><strong>function</strong> main(){<br>console.log(“1. Starting Script”);<br><strong>const</strong> data <strong>=</strong> getData();<br>console.log(`3. Data is currently ${JSON.stringify(data)}`);<br>console.log(“4. Script Ended”);<br>}<br>main();</pre><p>We can copy and paste this into a DevTools console to see the result. It matches our default model of “how code runs.”</p><h3>Identifying an Asynchronous Code Bloc</h3><p>The easiest asynchronous wrapper function is window.setTimeout(). It takes as arguments:</p><ul><li>a Function (the &quot;callback&quot; function)</li><li>a Number representing milliseconds</li></ul><p>The setTimeout() will wait the number of milliseconds and then execute the callback.</p><pre>setTimeout(() <strong>=&gt;</strong> console.log(‘Hello World!’), 2000);</pre><p>This says “Hello World!”… in 2 seconds. Try it out in the DevTools console!</p><p>Since this code is in an <em>asynchronous</em> container, JavaScript can do other work and <em>come back</em> when the work “on the back-burner is done.” If JavaScript <em>didn’t</em> have an asynchronous model, while you waited those 2 seconds, no gifs would animate or any background applications would run in your browser. Asynchronous execution makes browsers the exceedingly useful tools they are.</p><p>With our code written in this order:</p><pre>setTimeout(() <strong>=&gt;</strong> console.log(‘Hello World!’), 2000)</pre><pre>console.log(“No, me first”)</pre><p>The delay from setTimeout() means the line below returns first:</p><pre>No, me first</pre><pre>Hello World!</pre><p>JavaScript is so committed to trying to squeeze in work when it gets a chance that this has the exact same output!</p><pre>setTimeout(() <strong>=&gt;</strong> console.log(‘Hello World!’), 0) <em>// 0 Milliseconds!!</em></pre><pre>console.log(“No, me first”)</pre><p>The browser has &lt; 0 milliseconds (i.e. nanoseconds) to see if it can find any work to do!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3orieQoeTOzSK2ijbW%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3orieQoeTOzSK2ijbW%2Fsource.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3orieQoeTOzSK2ijbW%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="331" frameborder="0" scrolling="no"><a href="https://medium.com/media/161472e23947a72b7a6a3b7cf4f02e6e/href">https://medium.com/media/161472e23947a72b7a6a3b7cf4f02e6e/href</a></iframe><h3>Conclusion</h3><p>JavaScript in the browser has an asynchronous execution model. This fact has little impact when you’re writing simple code, but when you start doing work that might block the browser you’ll need to leverage asynchronous functions. Remember, these functions can be surprising and nearly every JavaScript developer sooner or later forgets to reckon with asynchrony.</p><p>While working asynchronously can be a bit of a headache for developers; it allows JavaScript to do other work whenever it has opportunity. An important, useful method which runs asynchronously are setTimeout() and fetch() among others.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FTur1zsvqIhm3S%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FTur1zsvqIhm3S%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FTur1zsvqIhm3S%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="261" frameborder="0" scrolling="no"><a href="https://medium.com/media/314138f1c1d341059b35f6326107cc1c/href">https://medium.com/media/314138f1c1d341059b35f6326107cc1c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5e65af45040" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lead me up the rails path]]></title>
            <link>https://medium.com/@galejscott/lead-me-up-the-rails-path-2d0b924e485?source=rss-bd22d070a3f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d0b924e485</guid>
            <category><![CDATA[route]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[path]]></category>
            <dc:creator><![CDATA[Scott Gale]]></dc:creator>
            <pubDate>Wed, 10 Jul 2019 14:35:49 GMT</pubDate>
            <atom:updated>2019-07-31T16:43:36.989Z</atom:updated>
            <content:encoded><![CDATA[<h4>A helpful guide to how rails paths work</h4><p>Rails is designed to be flexible; the result is that is usually a few ways to get the job done. Routes are a great example of this principle in a Rails app. I will review how to leverage built-in URL help methods instead of hard coding route paths in your app.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FtoSAzWmcwofew%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fbang-head-toSAzWmcwofew&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FtoSAzWmcwofew%2F200w.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="749" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ae6f742c5388bd69dfde2ea1c9850a6/href">https://medium.com/media/0ae6f742c5388bd69dfde2ea1c9850a6/href</a></iframe><h3>Paths vs Route Helpers</h3><p>What’s a real world difference between using hard coded paths compared with route helper methods? Let’s imagine that you have a meeting in London, and you want to get from one side of the city to the other. You have a couple different options:</p><ol><li>Traverse the streets on foot</li><li>Take an black cab/uber</li></ol><p>Walking is like hard coding your route’s path. Sure, it can work. However, it’s a slow, time consuming and easy to get wrong, and, if the meeting location changes, it will require quite a bit of manual work to adjust and get to the new destination.</p><p>Taking a cab/uber is like using a route helper: you can simply provide the address to the driver and let them navigate the city streets for you. It is faster than walking, and, if the address for the meeting changes while you’re en route, it’s not as difficult or slow to adjust.</p><ul><li><strong>Hard coded path:</strong> &quot;/posts/#{@post.id}&quot;</li><li><strong>Route helper: </strong>post_path(@post)</li></ul><p>So why would we want to use route helper methods as opposed to hard coding paths into the application? Below are a few of the key rationales:</p><ul><li>Route helpers are more dynamic since they are methods and not simply strings. This means that if something changes with the route there are many cases where the code itself won’t need to be changed at all.</li><li>Route helper methods help clean up the view and controller code and assist with readability. <em>On a side note, you cannot use these helper methods in your model files</em>.</li><li>It’s more natural to be able to pass arguments into a method as opposed to using string interpolation. For example, post_path(post, opt_in: true) is more readable than &quot;posts/&lt;%= post.id %&gt;?opt_in=true&quot;.</li><li>Route helpers translate directly into HTML-friendly paths. In other words, if you have any weird characters in your urls, the route helpers will convert them so they can be read properly by browsers. This includes items such as spaces or characters such as &amp;, %, etc.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FlyZUv9HTgkfx6%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Frail-lyZUv9HTgkfx6&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FlyZUv9HTgkfx6%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fda5917686966c3941f038941bbe1b4/href">https://medium.com/media/3fda5917686966c3941f038941bbe1b4/href</a></iframe><h3>Implementing Route Helpers</h3><p>To begin, I’m going to start with an application that has the MVC set up for posts, with index and show actions currently in place. Within the config/routes.rb file it would look like this:</p><ul><li>resources :posts, only: [:index, :show]</li></ul><p>If we ran rails routesin the terminal it will give the following output:</p><ul><li>posts GET /posts<strong>(</strong>.:format<strong>)</strong> posts#index</li><li>post GET /posts/:id<strong>(</strong>.:format<strong>)</strong> posts#show</li></ul><p>These four columns tell us everything that we’ll need in order to use the route helper methods. The breakdown is below:</p><ul><li><strong>Column 1</strong> — This column gives the prefix for the route helper methods. In the current application, posts and post are the prefixes for the methods that you can use throughout your applications. The two most popular method types are _path and _url. So if we want to render a relative link path to our posts&#39; index page, the method would be posts_path or posts_url. The difference between _path and _url is that _path gives the relative path and _url renders the full URL.</li></ul><p><strong>In general, it&#39;s best to use the </strong><strong>_path version so that nothing breaks if your server domain changes</strong>.</p><ul><li><strong>Column 2</strong> — This is the HTTP verb.</li><li><strong>Column 3</strong> — This column shows what the path for the route will be and what parameters need to be passed to the route. As you may notice, the second row for the show route calls for an ID. When you pass the resources method to the :showargument, it will automatically create this route and assume that you will need to pass the id into the URL string. Whenever you have id parameters listed in the path like this, you will need to pass the route helper method an ID, so an example of what our show route code would look like is post_path(@post). Notice how this is different than the index route of posts_path. We can ignore the (.:format) text for now.</li><li><strong>Column 4</strong> — This column shows the controller and action with a syntax of controller#action.</li></ul><p>Route helper methods become nice and predictable. If a Rails application was built with traditional routing conventions, you will see the CRUD controllers for newsletters, students, sales and coupons, you don&#39;t have to look up the routes to know that you could call the index URLs for each resource below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*xkDlZbrLw5fxUIH37UokFA.jpeg" /></figure><ul><li>Newsletters — newsletters_path</li><li>Students — students_path</li><li>Sales — sales_path</li><li>Coupons — coupons_path</li></ul><h3>link_to Method</h3><p>Rails is smart enough to know that if you pass in the postobject as an argument, it will automatically use the ID attribute, so our code linking thee index view to the show view could look like this:</p><p>&lt;% @posts.each do |post| %&gt;<br>&lt;div&gt;&lt;%= link_to post.title, post_path(post) %&gt;&lt;/div&gt;<br>&lt;% end %&gt;</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fe7eZRpz4JDkqc%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Frails-e7eZRpz4JDkqc&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fe7eZRpz4JDkqc%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="252" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e0dfd15d67e46a673132f025de3bba1/href">https://medium.com/media/0e0dfd15d67e46a673132f025de3bba1/href</a></iframe><p>We’re using the link_to method to automatically create an HTML a tag. If you open the browser and inspect the HTML element of the link, you would see the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mYvxL5hPbilxsXp0.png" /></figure><p>As you can see, even though we never added HTML code for the link — e.g., &lt;a href=&quot;...&quot;&gt;&lt;/a&gt; –– the link_to method rendered the correct tag for us.</p><h3>Using the :as option</h3><p>If for any reason you don’t like the naming structure for the methods or paths, you can customise each route easily. A common change is updating the path users go to in order to register for a site. Out of the box, the standard path would be /users/new. However, we would want something a little more readable, like /register. We could update ourconfig/routes.rb file with:</p><p>get &#39;/register&#39;, to: ‘users#new&#39;, as: ‘register&#39;</p><p>Now the application lets users navigate to /register to sign up, and you, the developer, can utilise your own custom register_path route helper throughout the app.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FotXUdOJgy623e%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fquote-quotes-train-otXUdOJgy623e&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FotXUdOJgy623e%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="315" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff9e1075d98e9f28fa71bb759512e5e/href">https://medium.com/media/aff9e1075d98e9f28fa71bb759512e5e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d0b924e485" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Iterating…]]></title>
            <link>https://medium.com/@galejscott/iterating-66f68062fe67?source=rss-bd22d070a3f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/66f68062fe67</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[archer]]></category>
            <dc:creator><![CDATA[Scott Gale]]></dc:creator>
            <pubDate>Wed, 19 Jun 2019 15:00:01 GMT</pubDate>
            <atom:updated>2019-07-31T16:42:43.643Z</atom:updated>
            <content:encoded><![CDATA[<h4>with the world’s greatest secret agent</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FtsGQgvmce0v4I%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FtsGQgvmce0v4I%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FtsGQgvmce0v4I%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="355" frameborder="0" scrolling="no"><a href="https://medium.com/media/b686dc9ddba1f62231f92ffde5911b2e/href">https://medium.com/media/b686dc9ddba1f62231f92ffde5911b2e/href</a></iframe><p>It seems so simple when you know how, but when starting out, many (myself included) have struggled to know which iterator to use when faced with having to iterate through an array (or hash — for the ease of my examples will be using arrays).</p><p>In order to iterate through an array, we have to take in a block. A block is defined by a variable enclosed by pipes “|variable|”; we can make the variable anything we want, but it’s good practice to singularise the name of the array (hoping the array, is named appropriately too). The variable is now acting as each element of the array.</p><p>To bang! or not to bang! That is the question. Here comes the inappropriateness! Do we want to use a destructive method or non-destructive one is a key question you need to ask yourself when picking the correct iterator. If you want to return the same array as which you started with (non-destructive) or to alter the returning array (destructive).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F13PR67zViZjXi%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F13PR67zViZjXi%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F13PR67zViZjXi%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="241" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bbef07f8ab935e35e3bfe01c951216a/href">https://medium.com/media/5bbef07f8ab935e35e3bfe01c951216a/href</a></iframe><p>We’ll start with the non-destructive iterators:</p><p>Let’s start simple, with .each. This iterator allows us to iterate through an array and run a method on each element of the array. It will output the result of our method, but leave the array in its original state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*kz9w-gMw47KcBFhzOetiEw.png" /></figure><p>Next up is .select, which is used when we want to pick out which elements of our array match with a certain criteria (.select usually asks a question rather than performing an action). It is also a non-destructive iterator, so while outputting a selection which matches the criteria, the array itself remains unchanged.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*JkLEGcnbiG1uFbU0ODYWlg.png" /></figure><p>The opposite of .select is .reject, which dependant on the criteria will remove the elements which match the criteria from its returning values, but will also leave the array in its original state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*5ExFVK9x2Ja0ybhx9X_QwA.png" /></figure><p>Similar to .select, there is .detect (also known as .find) which does the same thing as .select, but only returns the first element of the array which fits with our criteria and not all of the them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*ROu7x_RgJAd1ArFQP3OjVQ.png" /></figure><p>Lets move on to the destructive iterators:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FZ3AqPdOdolOFi%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FZ3AqPdOdolOFi%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FZ3AqPdOdolOFi%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="191" frameborder="0" scrolling="no"><a href="https://medium.com/media/c44886ea25a28f841801a11b0892724d/href">https://medium.com/media/c44886ea25a28f841801a11b0892724d/href</a></iframe><p>We have .map (also known as .collect) which will return an altered array from its original with whatever function has been applied. Sidenote: if you use a comparison (“==”) with .map rather than a method, an array of Booleans are returned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*g8DaU9wTvytfSgpR2I2MyQ.png" /></figure><p>Other destructive iterators, of which work similarly to .select and .reject, are .keep_if and .delete_if which dependant on the criteria, either keep within or delete from the new returning array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*gCska8KyJm_JcT9O5IaCng.png" /></figure><p>There is also .compact, which isn’t an iterator, but is a useful little method and will return a new array having removed all the nil values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*OUWnhkEtmQDkKGaRH20WjQ.png" /></figure><p>To bang! or not to bang!? We can make many non-destructive methods destructive by adding a bang! which if have studied the english language at all you probably know it as an exclamation mark! In effect, .select! is the same as .keep_if and .reject! is the same as .delete_if.</p><p>The bang! at the end of the method name, means the method will become destructive if it was previously non-destructive. The ruby language likes to have many options for each method, just look at .size, .count and .length all doing the same thing; counting the number of elements within an array. Duplicate methods make seem unnecessary to some, but Ruby is all about the ease and flow of the code; so is only happy to oblige.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F107ovk5owwWfJK%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F107ovk5owwWfJK%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F107ovk5owwWfJK%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="301" frameborder="0" scrolling="no"><a href="https://medium.com/media/5402e0d5dc8d54c9ffc69ae0db029ce1/href">https://medium.com/media/5402e0d5dc8d54c9ffc69ae0db029ce1/href</a></iframe><p>Iterating through arrays, can be done a variety of ways as I have described above, and some methods are designed for more specific actions. It’s wise to take note of which method/methods do what as they will make your life easier. And after all, isn’t that what ruby is designed to do; make you happy!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F146LIs4tfXysve%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F146LIs4tfXysve%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F146LIs4tfXysve%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae9a16a80ebf1a15901d10f12571099c/href">https://medium.com/media/ae9a16a80ebf1a15901d10f12571099c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66f68062fe67" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[puts “Hello World!”]]></title>
            <link>https://medium.com/@galejscott/puts-hello-world-fac31c1d51fd?source=rss-bd22d070a3f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fac31c1d51fd</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[pry]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[imposter-syndrome]]></category>
            <dc:creator><![CDATA[Scott Gale]]></dc:creator>
            <pubDate>Wed, 29 May 2019 13:39:47 GMT</pubDate>
            <atom:updated>2019-05-29T14:13:17.990Z</atom:updated>
            <content:encoded><![CDATA[<h4>And a couple of hard lessons learnt</h4><p>Almost definitely a cliché? Or is it a right of passage every newly budding programmer has had to type? A first step into the world of programming and now blogging.</p><p>I recently joined the newest cohort at <a href="https://medium.com/u/973c5cbfb09b">Flatiron School</a> in London. Where, in hindsight, I was not fully prepared for the challenges ahead of me when I walked through the door on day 1 a couple of weeks ago. So I want to share a couple of things I believe to be important when starting out in the world of programming.</p><p>On a side note, I seemed to have forgotten how exhausting learning is! Concentrating all day really takes it out of you. Or that just me?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FLTYT5GTIiAMBa%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FLTYT5GTIiAMBa%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FLTYT5GTIiAMBa%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="287" frameborder="0" scrolling="no"><a href="https://medium.com/media/41fec69b168ac266fea2173fd5536f67/href">https://medium.com/media/41fec69b168ac266fea2173fd5536f67/href</a></iframe><p>During the first week, the concept of “Imposter Syndrome” is almost drummed into you - the feeling that you might not belong, you have no idea what you are doing and at any moment someone will work this out; that you’re a fraud.</p><p>My background is the dog eat dog world of finance, where the opposite is expected, - “If you can’t spot the fool at the table, it’s you.” Everyone is out for number 1 at the end of the day. This new world of programming is completely different, it’s a team sport not an individual one. Everyone is offering help at every turn and we’re all on the same team! (It’s like being in Canada, compared to the London tube.)</p><p>“Imposter Syndrome” has been all about adopting the correct mindset, and accepting that its alright to admit you don’t have the answer or you’re not sure and that’s ok. Previously, that wasn’t really an option, it was seen as weakness to ask for assistance as you’re being judged on your output, which in turn, equated to your value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*1eYBAkQOU55OfOruMRQxPg.jpeg" /><figcaption><a href="http://examinedexistence.com/wp-content/uploads/2014/05/two-mindsets-fixed-and-growth-carol-dweck.jpg">http://examinedexistence.com/wp-content/uploads/2014/05/two-mindsets-fixed-and-growth-carol-dweck.jpg</a></figcaption></figure><p>Choosing to see criticism and failures as challenges and opportunities to learn will only help progress. As well as seeing success in others as opportunities to learn rather than signs of your own failure.</p><p>This all being said, it’s tough to maintain optimism. There are plenty of moments we’ve all felt like Moss, I’m sure.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FlNubxCPAPvSUw%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FlNubxCPAPvSUw%2Fsource.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FlNubxCPAPvSUw%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="251" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d2a981f04f79f345e213cdfd66e0653/href">https://medium.com/media/2d2a981f04f79f345e213cdfd66e0653/href</a></iframe><p>I’ve learnt to celebrate the little wins, because you quickly realise it’s just one little piece of the puzzle that is learning to code. We are all learning at different paces; topics that you find hard to grasp quickly, others pick up easily and vice versa. So don’t be too critical on yourself.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FcAlo9biOeDAkM%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FcAlo9biOeDAkM%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FcAlo9biOeDAkM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="243" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e11e4b3c90218156e4ede2c1131b634/href">https://medium.com/media/6e11e4b3c90218156e4ede2c1131b634/href</a></iframe><p>The second thing, which took me longer than it should have done, is TDD or Test Driven Development. I had too much reliance on the inbuilt test suites, as the ability to just type learn into terminal was very convenient at first.</p><p>But, at some point you have to take the training wheels off….</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FUsOoz5EYqRxXW%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FUsOoz5EYqRxXW%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FUsOoz5EYqRxXW%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="305" frameborder="0" scrolling="no"><a href="https://medium.com/media/75ee2a34d7423bdfeb96379fc9aed628/href">https://medium.com/media/75ee2a34d7423bdfeb96379fc9aed628/href</a></iframe><p>A shoutout to pry. This little hero allows to jump into your code and see what works and what doesn’t.</p><p>All in takes is you to use “require ‘pry’” and “binding.pry” and you can now test your methods, variables and whatever else within the pry console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*naaRrMh4o_1p7UbWZVrCpA.png" /></figure><p>My reliance on using the provided learn and rspec tests slowed progress and my increased my reluctance to test my methods whenever there wasn’t a test suite available to do so. Unfortunately, this is not how the real world operates and typing code at 100 miles an hour untested, with my current abilities is completely useless.</p><p>As it’s best not to follow the kid in the gif above, take a breath and use pry, enabling you to test each line of code you write. Debugging is an essential skill for programming, full stop. Even more so, when you’re starting out. Allowing smooth, cruising into Mod 2. Just remember to exit pry when you’re done!!!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FWDtCGLCXBAgow%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FWDtCGLCXBAgow%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FWDtCGLCXBAgow%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="218" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fb4a9a72fff746b8646de4309c56910/href">https://medium.com/media/9fb4a9a72fff746b8646de4309c56910/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fac31c1d51fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>