<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anthony Harvey</title>
    <description>The latest articles on DEV Community by Anthony Harvey (@anthonyharvey).</description>
    <link>https://dev.to/anthonyharvey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F74446%2F38641fe1-17ac-41eb-a951-c327b9a97844.jpg</url>
      <title>DEV Community: Anthony Harvey</title>
      <link>https://dev.to/anthonyharvey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anthonyharvey"/>
    <language>en</language>
    <item>
      <title>Ruby on Rails pluck method</title>
      <dc:creator>Anthony Harvey</dc:creator>
      <pubDate>Wed, 17 Jul 2024 12:30:00 +0000</pubDate>
      <link>https://dev.to/anthonyharvey/ruby-on-rails-pluck-method-3mp</link>
      <guid>https://dev.to/anthonyharvey/ruby-on-rails-pluck-method-3mp</guid>
      <description>&lt;p&gt;ActiveRecord is an important part of Ruby on Rails (&lt;a href="https://guides.rubyonrails.org/active_record_basics.html#what-is-active-record-questionmark" rel="noopener noreferrer"&gt;the M in MVC&lt;/a&gt;) and is an Object Relational Mapping system (ORM) that maps the objects in your app to tables in your database. By using ActiveRecord (or any ORM) the attributes of your objects and the relationships between them can be saved and retrieved from your database without writing SQL statements. One of the helpful methods that ActiveRecord provides to help facilitate this is &lt;code&gt;pluck&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the &lt;code&gt;pluck&lt;/code&gt; method?
&lt;/h2&gt;

&lt;p&gt;According to the &lt;a href="https://guides.rubyonrails.org/active_record_querying.html#pluck" rel="noopener noreferrer"&gt;Rails Guides&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/Calculations.html#method-i-pluck" rel="noopener noreferrer"&gt;&lt;code&gt;pluck&lt;/code&gt;&lt;/a&gt; can be used to pick the value(s) from the named column(s) in the current relation. It accepts a list of column names as an argument and returns an array of values of the specified columns with the corresponding data type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Put another way, &lt;code&gt;pluck&lt;/code&gt; let's you specify one or more columns that are defined on an ActiveRecord model and returns the values in an Array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT orders.id FROM orders&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passing more than one column argument returns a nested array with values for each of the columns in each of the inner arrays. In this example the values for &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;shipped&lt;/code&gt; will be in the inner arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT orders.id, orders.status FROM orders&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can achieve similar results using &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;, but &lt;code&gt;pluck&lt;/code&gt; allows you to just specify the column(s) you want and get that data back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; together like this produces the same results, but you can replace the above code with &lt;code&gt;pluck&lt;/code&gt; and avoid chaining multiple methods.&lt;/p&gt;

&lt;p&gt;Plus, it's a little more concise, easier to read and usually faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes &lt;code&gt;pluck&lt;/code&gt; faster than &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;When using &lt;code&gt;select&lt;/code&gt;, it builds entire ActiveRecord objects from the database, which can be costly for large objects and for large queries if there are a lot of objects retrieved. Plus, there's the added step of mapping over the results to return the data in the shape that you want.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pluck&lt;/code&gt;, on the other hand, skips creating ActiveRecord objects entirely and only returns the data from the columns that you specified.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;pluck&lt;/code&gt; to query multiple tables
&lt;/h2&gt;

&lt;p&gt;You can also use &lt;code&gt;pluck&lt;/code&gt; to query across joined tables as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:customer&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"orders.id, customers.first_name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;code&gt;pluck&lt;/code&gt; on Enumerables
&lt;/h2&gt;

&lt;p&gt;You can also use &lt;code&gt;pluck&lt;/code&gt; on Enumerables to return the values you want by specifying the key(s). You just have to &lt;code&gt;require&lt;/code&gt; ActiveSupport, which &lt;a href="https://guides.rubyonrails.org/active_support_core_extensions.html#pluck" rel="noopener noreferrer"&gt;extends Enumerable&lt;/a&gt; with the &lt;code&gt;pluck&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"David"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Rafael"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Aaron"&lt;/span&gt; &lt;span class="p"&gt;}].&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; ["David", "Rafael", "Aaron"]&lt;/span&gt;
&lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"David"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Rafael"&lt;/span&gt; &lt;span class="p"&gt;}].&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; [[1, "David"], [2, "Rafael"]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to not use &lt;code&gt;pluck&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One thing to keep in mind when using &lt;code&gt;pluck&lt;/code&gt; is if you already have access to an Array of ActiveRecord objects that you're retrieved from the database.&lt;/p&gt;

&lt;p&gt;Calling &lt;code&gt;pluck&lt;/code&gt; on that Array of ActiveRecord objects will trigger an additional query to the database. You can avoid this extra round trip to the database by using &lt;code&gt;map&lt;/code&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;order_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;completed_orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;status: &lt;/span&gt;&lt;span class="s2"&gt;"completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;completed_orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="c1"&gt;# .each will make a database call&lt;/span&gt;
    &lt;span class="n"&gt;order_ids&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# will trigger an unecessary database call&lt;/span&gt;
&lt;span class="n"&gt;completed_orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# the completed_orders array is already loaded in memory&lt;/span&gt;
&lt;span class="c1"&gt;# and we can just map over the array&lt;/span&gt;
&lt;span class="n"&gt;completed_orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recent Update to &lt;code&gt;pluck&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://github.com/rails/rails/pull/51565" rel="noopener noreferrer"&gt;Pull Request&lt;/a&gt; was introduced by &lt;a href="https://github.com/fatkodima" rel="noopener noreferrer"&gt;fatkodima&lt;/a&gt; and merged into Rails on April 13, 2024 to allow &lt;code&gt;ActiveRecord::Base#pluck&lt;/code&gt; to accept hash values!&lt;/p&gt;

&lt;p&gt;From the PR:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Before&lt;/span&gt;
&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:comments&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"posts.id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"comments.id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"comments.body"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# After&lt;/span&gt;
&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:comments&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;posts: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;comments: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/active_record_querying.html#pluck" rel="noopener noreferrer"&gt;RailsGuides: Finding by SQL - pluck&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/Calculations.html#method-i-pluck" rel="noopener noreferrer"&gt;Rails API Docs - Active Record Calculations - pluck&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://dev.tourl"&gt;anthonygharvey.com&lt;/a&gt; on July 16th, 2024.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Guard Clauses vs. Nested Conditionals</title>
      <dc:creator>Anthony Harvey</dc:creator>
      <pubDate>Sun, 28 Jul 2019 18:07:27 +0000</pubDate>
      <link>https://dev.to/anthonyharvey/guard-clauses-vs-nested-conditionals-3id0</link>
      <guid>https://dev.to/anthonyharvey/guard-clauses-vs-nested-conditionals-3id0</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;; a guard clause is a premature return (early exit) that "guards" against the rest of your code from executing if it's not necessary (based on criteria you specify).&lt;/p&gt;




&lt;p&gt;Soon after I started my career as a Ruby on Rails developer I learned about guard clauses and how they can improve code readability.&lt;/p&gt;

&lt;p&gt;Let's take a look at an example method with a standard &lt;code&gt;if/else&lt;/code&gt; statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="c1"&gt;# awesome code here&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method we're doing something that's common: evaluating a parameter and:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;executing some code if our expectations are met &lt;strong&gt;OR&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;executing some other code if our expectations are not met.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the parameter is &lt;code&gt;true&lt;/code&gt; we execute some "awesome code"&lt;/li&gt;
&lt;li&gt;if not, we just return &lt;code&gt;nil&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method can be shortened to only two lines of code by using a guard clause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;-- guard clause&lt;/span&gt;
  &lt;span class="c1"&gt;# awesome code here&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dev.to/anthonyharvey/ruby-if-unless-while-and-until-4f9a"&gt;unless method&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was as short example to illustrate the concept of a guard clause. Now let's look at an example where a guard clause can help eliminate a code smell: nested conditionals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_shipping_label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;outstanding_payments?&lt;/span&gt;
    &lt;span class="kp"&gt;nil&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;address_incomplete?&lt;/span&gt;
      &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_address_reminder&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;standard_shipping?&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_standard_shipping_label&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_priority_shipping_lable&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is hard to read and follow what's happening in my opinion (just look at how many levels of indention are in the nested &lt;code&gt;if/else&lt;/code&gt; statements!)&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring Nested Conditionals Using Guard Clauses
&lt;/h2&gt;

&lt;p&gt;We can use guard clauses to refactor the nested &lt;code&gt;if/else&lt;/code&gt; statements and make the method more readable and easier to follow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_shipping_label2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;outstanding_payments?&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_address_reminder&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;address_incomplete?&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_standard_shipping_label&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;standard_shipping?&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_priority_shipping_label&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like guard clauses for these reasons, but you should use them where it makes sense for you and your team.&lt;/p&gt;

&lt;p&gt;For example, some languages require explicit resource management and having a single exit point from a method would make sense to prevent memory leaks. However Ruby has &lt;a href="https://blog.heroku.com/incremental-gc" rel="noopener noreferrer"&gt;garbge collection&lt;/a&gt; to automatically collect unused objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Takeaway:
&lt;/h2&gt;

&lt;p&gt;Guard clauses &lt;strong&gt;guard&lt;/strong&gt; (protect) the rest of your code from executing if not necessary (based on certain criteria) and are placed at the top of a method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html" rel="noopener noreferrer"&gt;Refactoring.com - Replace Nested Conditional with Guard Clauses&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://refactoring.guru/replace-nested-conditional-with-guard-clauses" rel="noopener noreferrer"&gt;Refactoring.guru - Replace Nested Conditional with Guard Clauses&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://anthonygharvey.com/ruby/guard_clauses_vs_nested_if_statements" rel="noopener noreferrer"&gt;anthonygharvey.com&lt;/a&gt; on July 28th, 2019.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>fundamentals</category>
    </item>
    <item>
      <title>How to create a global .gitignore file</title>
      <dc:creator>Anthony Harvey</dc:creator>
      <pubDate>Sun, 07 Apr 2019 17:23:31 +0000</pubDate>
      <link>https://dev.to/anthonyharvey/how-to-create-a-global-gitignore-file-3n76</link>
      <guid>https://dev.to/anthonyharvey/how-to-create-a-global-gitignore-file-3n76</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Open the Terminal&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;touch ~/.gitignore_global&lt;/code&gt; to create a global .gitignore file in the home directory&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;open ~/.gitignore_global&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;.vscode/&lt;/code&gt; and any other files or directories that you want to ignore and not include in project-specific &lt;code&gt;.gitignore&lt;/code&gt; files (don't forget to save!)&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;git config --global core.excludesfile ~/.gitignore_global&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it! Using &lt;code&gt;.gitignore_global&lt;/code&gt; will let you customize your editor without having to edit and commit changes to the &lt;code&gt;.gitignore&lt;/code&gt; file for every single project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Editor Specific Example
&lt;/h2&gt;

&lt;p&gt;Visual Studio Code (and other editors) have the ability to make customizations to fit your needs; especially through &lt;a href="https://marketplace.visualstudio.com/" rel="noopener noreferrer"&gt;extensions&lt;/a&gt;. I recently started using the &lt;a href="https://marketplace.visualstudio.com/items?itemName=johnpapa.vscode-peacock" rel="noopener noreferrer"&gt;Peacock extension&lt;/a&gt; to quickly distinguish between multiple VS Code instances that I may have open at one time. You can customize Peacock to change the color of your Activity Bar, Status Bar and/or the Title Bar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn3z7gz4nppkrfkv0xq6r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn3z7gz4nppkrfkv0xq6r.png" alt="VS Code Peacock extension windows"&gt;&lt;/a&gt;image credit: &lt;a href="https://github.com/johnpapa/vscode-peacock" rel="noopener noreferrer"&gt;johnpapa&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fgxyo4cdqlfigt2p2prl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fgxyo4cdqlfigt2p2prl8.png" alt="I like the subtlety of only changing the Title Bar color"&gt;&lt;/a&gt;I like the subtlety of only changing the Title Bar color&lt;/p&gt;

&lt;p&gt;This will create a &lt;code&gt;settings.json&lt;/code&gt; inside of a &lt;code&gt;.vscode&lt;/code&gt; folder. While this is applicable to your local editor, it may not be appropriate to commit to the repository (after all, your teammates may be using Atom, Sublime or Vim). Instead of adding these editor specific files to &lt;code&gt;.gitignore&lt;/code&gt; for every project, you can create a &lt;code&gt;global .gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://help.github.com/en/articles/ignoring-files" rel="noopener noreferrer"&gt;Github - Ignoring Files&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://anthonygharvey.com/productivity/how_to_create_a_global_gitignore_file" rel="noopener noreferrer"&gt;anthonygharvey.com&lt;/a&gt; on April7th, 2019.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Reverse a String in JavaScript</title>
      <dc:creator>Anthony Harvey</dc:creator>
      <pubDate>Sat, 09 Mar 2019 18:39:08 +0000</pubDate>
      <link>https://dev.to/anthonyharvey/how-to-reverse-a-string-in-javascript-2j40</link>
      <guid>https://dev.to/anthonyharvey/how-to-reverse-a-string-in-javascript-2j40</guid>
      <description>&lt;p&gt;Reversing a string in JavaScript may seem like a simple task, but there are several steps involved and, in the context of a technical interview, it provides opportunities to showcase your knowledge of JavaScript and trade-offs / edge cases considered.  It is good practice to make sure you understand the problem and any constraints before beginning to write code.&lt;/p&gt;

&lt;p&gt;Some of the constraints may be given to you upfront, such as: “Write an algorithm that reverses a string in place &lt;strong&gt;without&lt;/strong&gt; using built-in functions like &lt;code&gt;.reverse()&lt;/code&gt; or &lt;code&gt;.charAt()&lt;/code&gt;.  It is always a good idea to ask clarifying questions before jumping in and attempting to solve the problem.  In this case we are asked to reverse a string.  Some questions we could ask include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Are there any conditions for the string that will be passed?&lt;/li&gt;
&lt;li&gt;Will the string contain regular or special characters?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start with the least amount of constraints and work our way up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a string input, return  a string with its characters in the reversed order of the input string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this challenge, we are not provided with any restrictions on the functions we can use.  We can also ask our clarifying questions if the string input will contain any special characters or if there are any conditions.  For this challenge, let's assume the answer is the input will only contain regular ASCII characters.&lt;/p&gt;

&lt;p&gt;Now that we have asked our clarifying questions, you can restate the problem with the new information to buy yourself more time to think and get used to communicating your thought process out loud.  You could say something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a string input of &lt;strong&gt;only ASCII characters&lt;/strong&gt;, I want to create a function that returns a &lt;strong&gt;new string&lt;/strong&gt; with its characters in the reverse order of the input string.  I want to return a new string because I want to avoid mutating the original string input&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that you have restated the problem as you understand it based on the responses to your clarifying questions, this may be a good point to write some simple examples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;olleh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;!dlrow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tpircSavaJ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we understand the problem, the input and expected output, we can break it down into steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;convert the string into an array with each character in the string as an element&lt;/li&gt;
&lt;li&gt;reverse the order of the elements in the array&lt;/li&gt;
&lt;li&gt;convert the reversed array back into a string&lt;/li&gt;
&lt;li&gt;return the reversed string
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// step 1&lt;/span&gt;
  &lt;span class="nx"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;// step 2&lt;/span&gt;
  &lt;span class="nx"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// step 3&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt;  &lt;span class="c1"&gt;//step 4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we can test the function with the examples we created above, again communicating our thought process out loud by saying something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a string &lt;code&gt;hello&lt;/code&gt; as an input, the function would:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;first split the string on each character, convert it to an array with each character as an element and assign the array to the variable &lt;code&gt;answer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;reverse the order of the elements in the &lt;code&gt;answer&lt;/code&gt; array&lt;/li&gt;
&lt;li&gt;join the elements in the reversed &lt;code&gt;answer&lt;/code&gt; array into a string and reassign it to the &lt;code&gt;answer&lt;/code&gt; variable&lt;/li&gt;
&lt;li&gt;return the &lt;code&gt;answer&lt;/code&gt; variable, which is now a reversed string of the input&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that the function works, we can refactor it to make it more DRY.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;spread syntax&lt;/a&gt; inside of the array literal notation as an alternative to the &lt;code&gt;.split()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's suppose we are given the same problem but with more constraints&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a string input, return  a string with its characters in the reversed order of the input string without using built-in functions like &lt;code&gt;.reverse()&lt;/code&gt;, &lt;code&gt;.charAt()&lt;/code&gt;, the spread syntax; and without using an array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For the sake of brevity, let's assume the same answers to our clarifying questions above.  We can use the same approach for the first problem, but just with different steps given the new constraints.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create a variable to serve as the return value and set it as an empty string, call it &lt;code&gt;reversed&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;loop through the characters in the input string&lt;/li&gt;
&lt;li&gt;Within each loop iteration, add the character to the &lt;strong&gt;beginning&lt;/strong&gt; of the &lt;code&gt;reversed&lt;/code&gt; string&lt;/li&gt;
&lt;li&gt;return &lt;code&gt;reversed&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The reason we want to add each character to the beginning of the &lt;code&gt;reversed&lt;/code&gt; string is because if we added them to end of the string, we would just be reconstructing the input string as is and we want the return value to be a reversed string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;  &lt;span class="c1"&gt;// step 1&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// step 2&lt;/span&gt;
    &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// step 3&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// step 4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In step 3 we are just concatenating the character in each iteration with the reversed string, (being careful to add it to the beginning) and reassigning the concatenated string back to the reversed variable.  In other words, starting with the character and adding (concatenating) the reversed string to the end of it; then reassigning this combined string to the &lt;code&gt;reversed&lt;/code&gt; variable.  &lt;/p&gt;

&lt;p&gt;If we put &lt;code&gt;reversed = reversed + char&amp;lt;&lt;/code&gt;, that would be the opposite of what we want and would just reconstruct the input string (&lt;code&gt;reverse('hello') === 'hello'&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fanthonygharvey.com%2Fassets%2Fimg%2Freverse_string_concatenation.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fanthonygharvey.com%2Fassets%2Fimg%2Freverse_string_concatenation.png" alt="Reversing a string using a for..of loop and concatenation"&gt;&lt;/a&gt;&lt;/p&gt;
Reversing a string using a for..of loop and concatenation



&lt;p&gt;Also, step 2 uses the &lt;code&gt;for...of&lt;/code&gt; statement introduced in ES2015.  The syntax is easier to read and it creates a loop that iterates over iterable objects.  If the interviewer asks you to not use ES2015, you can always use a traditional &lt;code&gt;for&lt;/code&gt; loop; but if you do not have that restriction, I think using &lt;code&gt;for...of&lt;/code&gt; loops are better because the syntax is simpler and they reduce the chances of unintentional errors being introduced (like using a comma instead of a semi-colon to separate the initial expression, condition and incremental expression).&lt;/p&gt;

&lt;p&gt;For our last challenge, let's suppose we are given the same problem but the input strings may contain more than just ASCII characters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a string input of Unicode characters, return  a string with its characters in the reversed order of the input string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this challenge, the input string will be more than 7 bit ASCII characters and contain 8 bit Unicode characters (see this &lt;a href="https://www.youtube.com/watch?v=5aJKKgSEUnY" rel="noopener noreferrer"&gt;video&lt;/a&gt; for a great explanation of the differences between ASCII and Unicode).&lt;/p&gt;

&lt;p&gt;If we use our initial function to reverse a string containing Unicode characters, we get weird and unexpected results.  Let’s include an emoji in the string because, why not?!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JS is fun! 😄&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; �� !nuf si SJ&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can get around this by using the &lt;code&gt;Array.from()&lt;/code&gt; method to create a new array from an array-like or iterable object (in our case a string).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JS is fun! 😄&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; 😄 !nuf si SJ&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now that you have learned about different ways to approach the reverse a string algorithm in JavaScript, take a look at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.freecodecamp.org/10-common-data-structures-explained-with-videos-exercises-aaff6c06fb2b" rel="noopener noreferrer"&gt;10 Common Data Structures Explained with Videos + Exercise&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/t/basecs"&gt;BaseCS Video Series&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codenewbie.org/basecs" rel="noopener noreferrer"&gt;BaseCS podcast on CodeNewbie&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more reading, check out algorithm tutorials on &lt;a href="https://coderbyte.com/challenges?a=true" rel="noopener noreferrer"&gt;Coderbyte&lt;/a&gt; and the sorting algorithms animations by &lt;a href="https://www.toptal.com/developers/sorting-algorithms" rel="noopener noreferrer"&gt;Toptal&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this was helpful!  Let me know if you have any questions or comments.  You can follow me on &lt;a href="https://www.linkedin.com/in/anthonygharvey" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://github.com/anthonygharvey" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, &lt;a href="https://twitter.com/anthonyharvey" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and on my &lt;a href="https://anthonygharvey.com" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://anthonygharvey.com/algorithms/how_to_reverse_a_string_in_javascript" rel="noopener noreferrer"&gt;anthonygharvey.com&lt;/a&gt; on July 28th, 2018&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Ruby If, Unless, While and Until</title>
      <dc:creator>Anthony Harvey</dc:creator>
      <pubDate>Thu, 07 Mar 2019 02:38:23 +0000</pubDate>
      <link>https://dev.to/anthonyharvey/ruby-if-unless-while-and-until-4f9a</link>
      <guid>https://dev.to/anthonyharvey/ruby-if-unless-while-and-until-4f9a</guid>
      <description>&lt;p&gt;Ruby offers conventional control structures that are found in most common programming languages like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt;, however, Ruby also provides less common options like &lt;code&gt;until&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt;.  These control structures may seem weird at first (“who talks like that?”), but they have their advantages.&lt;/p&gt;

&lt;h2&gt;
  
  
  If Statement
&lt;/h2&gt;

&lt;p&gt;Let’s start with a basic &lt;code&gt;if&lt;/code&gt; statement.  Here we have a &lt;code&gt;Movie&lt;/code&gt; class with several attributes and functionality to only update a movie’s title if the &lt;code&gt;in_progress&lt;/code&gt; attribute is &lt;code&gt;true&lt;/code&gt;; easy enough.  If &lt;code&gt;in_progress&lt;/code&gt; is true, then the &lt;code&gt;title&lt;/code&gt; attribute can be assigned a new value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:in_progress&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:budget&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@in_progress&lt;/span&gt;
      &lt;span class="vi"&gt;@title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_title&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s imagine if the attribute was &lt;code&gt;finalized&lt;/code&gt; instead of &lt;code&gt;in_progress&lt;/code&gt;.  It may be tempting to put a &lt;code&gt;not&lt;/code&gt; or &lt;code&gt;!&lt;/code&gt; in front of it and call it a day.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:finalized&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:budget&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="vi"&gt;@finalized&lt;/span&gt;
      &lt;span class="vi"&gt;@title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_title&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This if statement is valid but is a little more verbose than it has to be.  An alternative (and slightly more concise) way would be to use &lt;code&gt;unless&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:finalized&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:budget&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="vi"&gt;@finalized&lt;/span&gt;
      &lt;span class="vi"&gt;@title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_title&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;unless&lt;/code&gt;, the body of the statement (&lt;code&gt;@title = new_title&lt;/code&gt;) is only executed if the condition (&lt;code&gt;@finalized&lt;/code&gt;) is &lt;code&gt;false&lt;/code&gt;.  Using &lt;code&gt;unless&lt;/code&gt; comes with a couple of benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It’s slightly shorter that using &lt;code&gt;if not&lt;/code&gt; or &lt;code&gt;if !&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It may become a little easier to read and understand once you get used to it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second point is important.  Reading an &lt;code&gt;unless&lt;/code&gt; statement may seem weird at first (when I first encountered it in a production app I had to read it as “if not” until I got used to it).  But after a while it becomes less awkward and just another way to do a negative, or backwards, if statement.&lt;/p&gt;

&lt;p&gt;There’s no “right” way to do it; it’s just a preference.  The difference between an &lt;code&gt;if not&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt; is like saying:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;“It is &lt;code&gt;not true&lt;/code&gt; that an apple is an orange”&lt;/li&gt;
&lt;li&gt;“An apple is not an orange”&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  While and Until
&lt;/h2&gt;

&lt;p&gt;Similar to &lt;code&gt;unless&lt;/code&gt; is the negative version of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;until&lt;/code&gt; is the negative version of &lt;code&gt;while&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop will keep looping &lt;strong&gt;while&lt;/strong&gt; the condition (&lt;code&gt;! download.is_finished?&lt;/code&gt;) is &lt;code&gt;true&lt;/code&gt;.  In this example, the condition reads “while not download is finished (execute the code block)”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_finished?&lt;/span&gt;
  &lt;span class="n"&gt;spinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keep_spinning&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;until&lt;/code&gt; loop will keep looping &lt;strong&gt;until&lt;/strong&gt; the condition (&lt;code&gt;download.is_finished?&lt;/code&gt;) is &lt;code&gt;true&lt;/code&gt;.  In this example, the condition reads “until download is finished (execute the code block)”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;until&lt;/span&gt; &lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_finished?&lt;/span&gt;
  &lt;span class="n"&gt;spinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keep_spinning&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, it’s a matter of preference; but using &lt;code&gt;until&lt;/code&gt; became clearer once I understood the difference and became used to it.&lt;/p&gt;

&lt;p&gt;We can also make these examples a little more concise and readable.  Notice how the code block of the statements are only one line?  Because it’s only one one line and is pretty short, we can condense the statements to just a single line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;spinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keep_spinning&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_finished?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;spinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keep_spinning&lt;/span&gt; &lt;span class="k"&gt;until&lt;/span&gt; &lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_finished?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as before, it’s a matter of preference, but to me using &lt;code&gt;until&lt;/code&gt; is clearer and reads a little better.  &lt;/p&gt;

&lt;p&gt;That’s part of the reason I really like writing code in Ruby, there are multiple ways to do the same thing.  The language provides tools you need to express yourself and does its best to stay out of your way.  Does &lt;code&gt;not if&lt;/code&gt; make more sense to you or does writing &lt;code&gt;unless&lt;/code&gt; statements feel more natural?  Either way is fine because Ruby provides both options!  Same thing with &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://anthonygharvey.com/ruby/ruby_if,_unless,_while_and_until" rel="noopener noreferrer"&gt;anthonygharvey.com&lt;/a&gt; on March 5th, 2019.&lt;/em&gt;&lt;/p&gt;

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