<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Charlie Kozey</title>
    <description>The latest articles on Forem by Charlie Kozey (@charliekozey).</description>
    <link>https://forem.com/charliekozey</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%2F840139%2F08f9394f-d96d-405e-8d19-a75b1fb45ddd.jpeg</url>
      <title>Forem: Charlie Kozey</title>
      <link>https://forem.com/charliekozey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/charliekozey"/>
    <language>en</language>
    <item>
      <title>Demystifying algorithms</title>
      <dc:creator>Charlie Kozey</dc:creator>
      <pubDate>Thu, 21 Apr 2022 17:08:00 +0000</pubDate>
      <link>https://forem.com/charliekozey/demystifying-algorithms-4bj8</link>
      <guid>https://forem.com/charliekozey/demystifying-algorithms-4bj8</guid>
      <description>&lt;p&gt;Algorithms have an intimidating reputation for programmers and non-programmers alike. This post is meant to 1) provide a high-level overview of what an algorithm is (with a non-techy, hopefully easy-to-grasp example) and 2) briefly touch on what makes algorithms important.&lt;/p&gt;

&lt;p&gt;In the simplest terms, algorithms are just a series of steps executed in a specific order. Algorithms can be run by computers, of course, but they can also be done by hand. An example of an algorithm that most people have had experience with from an early age is long division.&lt;/p&gt;

&lt;p&gt;The long division algorithm looks something like this (Follow along with a pencil and paper if you'd like! Or, even better, hide this tab and try to write out the steps yourself. It can be a useful exercise to articulate the steps of a process that has become automatic for you, because that's what you'll have to do when translating steps into code.):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Set up the dividend (the number &lt;em&gt;being divided&lt;/em&gt;) and divisor (the number that is &lt;em&gt;divided by&lt;/em&gt;) in the bracket: dividend inside and divisor on the left. &lt;strong&gt;&lt;em&gt;Example: 42 on the left, 1000 inside bracket.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Examine the dividend one digit at a time, starting from the left, and check to see if the divisor can fit into that number at least once. (Let's call that number the fragment). If not, expand the fragment by one digit to the right and check again. &lt;strong&gt;&lt;em&gt;1 = too small. 10 = too small. 100 = big enough.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once you reach a fragment that can be divided by the divisor, put the quotient (the nearest whole number) on top of the bracket above the fragment's final digit. &lt;strong&gt;&lt;em&gt;100/42 = 2&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiply the quotient by the divisor, and place the result directly underneath the fragment, lining up the corresponding place-value columns. &lt;strong&gt;&lt;em&gt;42 * 2 = 84&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Subtract the result from the fragment. &lt;strong&gt;&lt;em&gt;100 - 84 = 16&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat steps 2-5 with the result found in step 5. If the result of step 5 is too small, bring the next digit of the dividend down and append it to the result of step 5. If at any point you run out of digits in the dividend, add a decimal point and as many zeros as necessary to the dividend. Also add a decimal point in the quotient in the same position. &lt;strong&gt;&lt;em&gt;16 = too small. 160 = big enough. 160/42 = 3. 3 * 42 = 126. 160 - 126 = 34. 34 = too small. 340 = big enough. 340/ 42 = 8&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stop when you reach a number that is evenly divided into your divisor, when you reach a repeating decimal, or when you have enough decimal places for your precision needs. &lt;strong&gt;&lt;em&gt;23.8 is precise enough for me.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At a fundamental level, any algorithm you can think of—search algorithms, recommendation algorithms, routing algorithms (and the list goes on...)—works exactly in this step-by-step manner. On a practical level, technical interviews for coding jobs often emphasize algorithms. It's an important skill to have if you want a job. Like most things, working with algorithms is a skill that you can improve with practice. There are many free resources online for this purpose.&lt;/p&gt;

&lt;p&gt;And on a societal level, it's important to think critically about algorithms and their real impact on people's lives. Like a lot of terms in, for example, finance, there’s a hand-wavey mystique about the word "algorithm." This mystique helps to maintain a status quo where insiders have a monopoly on knowledge and power. Moreover, because algorithms are iterative, their results are compounding. Small tweaks in their initial state can produce big changes in the output. Finally, an important implication of point 2 is that whatever biases are coded in by the programmers at the outset—however minor or unintentional—can have a huge impact on the final result (see, for example, racial biases in facial recognition algorithms). Algorithms are not a source of truth, which is especially important to remember since many people believe that if "a computer calculated it," it must be objectively true.&lt;/p&gt;

</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>How to handle irregular plurals in Ruby on Rails</title>
      <dc:creator>Charlie Kozey</dc:creator>
      <pubDate>Thu, 31 Mar 2022 19:10:28 +0000</pubDate>
      <link>https://forem.com/charliekozey/how-to-handle-irregular-plurals-in-ruby-on-rails-2f0l</link>
      <guid>https://forem.com/charliekozey/how-to-handle-irregular-plurals-in-ruby-on-rails-2f0l</guid>
      <description>&lt;p&gt;Join me as I wander down the rabbit hole of word-nerdery in Rails!&lt;/p&gt;

&lt;p&gt;One of the key philosophies of Rails is "convention over configuration," which makes it easier for programmers to establish relationships between items in a more or less intuitive way. Consider for a moment what the alternative would be: an emphasis on &lt;em&gt;configuration&lt;/em&gt; would require explicit instructions to the application in order for it to understand that, for example, the &lt;code&gt;Student&lt;/code&gt; class represents the data in the &lt;code&gt;students&lt;/code&gt; table. However, Rails' emphasis on &lt;em&gt;convention&lt;/em&gt; establishes the relationship based on the singular-to-plural relationship of &lt;code&gt;student&lt;/code&gt; to &lt;code&gt;Student&lt;/code&gt;. Provided programmers follow the agreed-upon convention, it all happens under the hood.&lt;/p&gt;

&lt;p&gt;This is all well and good, but what happens when the thing you want to keep track of in your database is &lt;em&gt;not&lt;/em&gt; a regular noun? Say you have a lot of mice to keep track of. If you create a "Mouse" class, will it be expecting a table called "Mouses?"&lt;/p&gt;

&lt;p&gt;Fortunately, it will not. Rails has a component called Active Support that takes care of a lot of the irregular cases for you.&lt;/p&gt;

&lt;p&gt;FYI: "inflection" is a linguistic term that describes the transformation of a part of a word (usually an ending) depending on its usage and context, for example, to differentiate number, gender, or tense. Verb conjugation is a subtype of inflection; inflections on other parts of speech are called declensions. So Active Support takes care of irregular plurals, but also a wider range of irregular word transformations.&lt;/p&gt;

&lt;p&gt;But what about the cases that Active Support doesn't know about? Or the cases it gets wrong? (Or at least, wrong for your purposes—I'm chill about what's right and wrong when it comes to grammar. You do you.) Say, for example, you are building a networking platform for graduates of a fancy university, and it's very important to your boss that the plurals of "alumna" and "alumnus" have their traditional Latin endings, "alumnae" and "alumni." True, these terms will only show up in the back end for you and your team to see, but let's say your boss is a stickler for details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fougz5aqikvidgfe8hp0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fougz5aqikvidgfe8hp0o.png" alt="Active Support pluralizes "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In that case, you can customize Active Support's inflections for your app. As of Rails v7, those customizations are often stored in a file called &lt;code&gt;/config/environment.rb&lt;/code&gt;, like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foew8swwtw7amlwzbx13h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foew8swwtw7amlwzbx13h.png" alt="ActiveSupport::Inflector.inflections(:en) do |inflect|&amp;lt;br&amp;gt;
  inflect.irregular 'alumna', 'alumnae'&amp;lt;br&amp;gt;
  inflect.irregular 'alumnus', 'alumni'&amp;lt;br&amp;gt;
end"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This process is also useful and important if you're coding in a language besides English, which is a topic that warrants a blog post of its own (at the least).&lt;/p&gt;

&lt;p&gt;As a bonus, Active Support includes other built-in methods that are helpful for modifying words in various ways, including changing from &lt;code&gt;snake_case&lt;/code&gt; to &lt;code&gt;camelCase.&lt;/code&gt; A list of these methods can be found &lt;a href="https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Another use case for Active Support is to create custom rules for inputs. For example, if your app has a list of user-inputted names, and you use the method &lt;code&gt;capitalize()&lt;/code&gt; on the inputs for uniformity's sake it successfully turns "vinay patel" to "Vinay Patel." But it also turns "Phil MacDougal" to "Phil Macdougal," which is incorrect and also a bummer for Phil, who's probably tired of that happening to his name. So you'd want to add a custom rule to Active Support that exempts names starting with "Mac", "Mc", "La", "De," etc.&lt;/p&gt;

&lt;p&gt;Good luck and have fun wrangling words in Ruby! &lt;/p&gt;

&lt;h3&gt;
  
  
  Sources consulted/further reading
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html" rel="noopener noreferrer"&gt;Rails API docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/ruby-string-capitalize-method/" rel="noopener noreferrer"&gt;GeeksforGeeks on capitalize()&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/59344290/rails-g-controller-with-irregular-plural" rel="noopener noreferrer"&gt;This StackOverflow post on irregular plurals in rails (a little outdated)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/active_support_core_extensions.html" rel="noopener noreferrer"&gt;Rails Guides: Active Support&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@lizzardl/naming-conventions-in-rails-ea5fe2f2a51" rel="noopener noreferrer"&gt;Naming Conventions in Rails&lt;/a&gt;&lt;/p&gt;

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