<?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: Caio Andrade</title>
    <description>The latest articles on DEV Community by Caio Andrade (@caioertai).</description>
    <link>https://dev.to/caioertai</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%2F234156%2F969e47a3-b67d-4463-90e9-280c54865fc3.jpg</url>
      <title>DEV Community: Caio Andrade</title>
      <link>https://dev.to/caioertai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/caioertai"/>
    <language>en</language>
    <item>
      <title>Rails i18n - Show translation_missing class style in production only</title>
      <dc:creator>Caio Andrade</dc:creator>
      <pubDate>Thu, 01 Oct 2020 22:36:19 +0000</pubDate>
      <link>https://dev.to/caioertai/rails-i18n-show-translationmissing-class-style-in-production-only-5f5c</link>
      <guid>https://dev.to/caioertai/rails-i18n-show-translationmissing-class-style-in-production-only-5f5c</guid>
      <description>&lt;p&gt;The &lt;code&gt;translation_missing&lt;/code&gt; class can be pretty useful in development to expose all the instances of missing &lt;code&gt;i18n&lt;/code&gt; keys in your app. Rails adds a &lt;code&gt;&amp;lt;span class="missing_translation" &amp;gt;&amp;lt;/span&amp;gt;&lt;/code&gt; around every use of his helper method &lt;code&gt;t&lt;/code&gt; that doesn't have a matching key in the YAML of the current locale.&lt;/p&gt;

&lt;p&gt;To help expose to the team every instance of missing translations, we recently started using a simple CSS class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.translation_missing&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&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;So we could all see red outlines on every instance of missing key:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IuNr-VaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d4ngahv3jm2pal6v5ho1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IuNr-VaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d4ngahv3jm2pal6v5ho1.png" alt="Alt Text" width="497" height="82"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The issue
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This would also show those outline in production! 😱&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most of the solutions I found in StackOverflow were either outdated or too complex to be trustworthy.&lt;/p&gt;

&lt;p&gt;I decided to read how those span classes are created by Rails to try and find a fix, but after my third monkey patch to Rail's &lt;code&gt;ActionView&lt;/code&gt; I realized that a simpler approach might be in order. &lt;/p&gt;
&lt;h2&gt;
  
  
  The simple fix
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create an &lt;code&gt;error_styles&lt;/code&gt; helper&lt;/strong&gt; that returns a &lt;code&gt;&amp;lt;style&amp;gt;&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="c1"&gt;# app/helpers/errors_helper.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ErrorsHelper&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;error_styles&lt;/span&gt;
    &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;style&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&amp;lt;~&lt;/span&gt;&lt;span class="no"&gt;CSS&lt;/span&gt;&lt;span class="sh"&gt;
        .translation_missing {
          outline: 1px solid red;
        }
&lt;/span&gt;&lt;span class="no"&gt;      CSS&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;And add it to your application layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- app/views/layouts/application.html.erb --&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!---- Somewhere in your &amp;lt;head&amp;gt; ----&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;error_styles&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;development?&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rails</category>
    </item>
    <item>
      <title>An ELI5 version of REGEX and NFA</title>
      <dc:creator>Caio Andrade</dc:creator>
      <pubDate>Thu, 06 Aug 2020 03:20:15 +0000</pubDate>
      <link>https://dev.to/caioertai/an-eli5-explanation-to-regex-and-nfa-2eaf</link>
      <guid>https://dev.to/caioertai/an-eli5-explanation-to-regex-and-nfa-2eaf</guid>
      <description>&lt;p&gt;You have 🤖 a robot (our automaton) that will “walk” through the REGEX dungeon, room by room 🏚 (character by character). Now imagine &lt;strong&gt;each room is locked&lt;/strong&gt;, and in order &lt;strong&gt;to access the next one it needs to collect from the string the key (or keys) character&lt;/strong&gt; that unlocks the next REGEX door.&lt;/p&gt;

&lt;p&gt;If the robot reaches the end of the REGEX, it’s free and it's a match!, if it gets stuck with a character that doesn’t open the next door it gets kicked to the start of the REGEX and needs to try again from the next string character. So if it has the string: &lt;code&gt;"budega"&lt;/code&gt;, and it fails at "b", it will try to open the REGEX again using &lt;code&gt;"udega"&lt;/code&gt; (no matter how much progress it made on the previous dungeon).&lt;/p&gt;

&lt;p&gt;If a robot is trying to match the dungeon &lt;code&gt;/there/&lt;/code&gt;, it will need a set of keys that are able to unlock each of its 5 doors (&lt;code&gt;t&lt;/code&gt;, &lt;code&gt;h&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, &lt;code&gt;r&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;) in perfect succession:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I give it &lt;code&gt;”there”&lt;/code&gt;. It will ofc use all the keys and successfully reach the end.&lt;/li&gt;
&lt;li&gt;If I give it &lt;code&gt;”here”&lt;/code&gt; it never even starts, since it would never be able to unlock the first door. It would try &lt;code&gt;"here"&lt;/code&gt;, then &lt;code&gt;"ere"&lt;/code&gt;, then &lt;code&gt;"re"&lt;/code&gt;, then &lt;code&gt;"e"&lt;/code&gt; and leave the dungeon feeling sad 🙁.&lt;/li&gt;
&lt;li&gt;If I give it &lt;code&gt;”then go over there”&lt;/code&gt; the robot would probably feel very confident going over the initial &lt;code&gt;“the”&lt;/code&gt; only to get stuck with an &lt;code&gt;“n”&lt;/code&gt; that can’t unlock the next door &lt;code&gt;r&lt;/code&gt; and get kicked to try all over again with &lt;code&gt;"hen go over there”&lt;/code&gt;. It would then successively fail in the first character until it got to try with &lt;code&gt;"there”&lt;/code&gt;, and finally be happy again. 🎉&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some doors can be opened with a varying number of characters (quantifiers) or different ones (ranges).&lt;/p&gt;

&lt;p&gt;A robot in the &lt;code&gt;/[hm]el+ow?/&lt;/code&gt; dungeon has 5 doors (characters) to cross: &lt;code&gt;[hm]&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, &lt;code&gt;l+&lt;/code&gt;, &lt;code&gt;o&lt;/code&gt; and &lt;code&gt;w?&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;“hello”&lt;/code&gt; would be a match since the first door can be opened with either an &lt;code&gt;"h"&lt;/code&gt; or an &lt;code&gt;"m"&lt;/code&gt;. And the &lt;code&gt;l+&lt;/code&gt; door would use both ls, leaving the robot with an &lt;code&gt;“o”&lt;/code&gt; to use in the next door &lt;code&gt;o&lt;/code&gt;. The last door &lt;code&gt;w?&lt;/code&gt; can be opened with 0 or 1 valid keys. It has 0, so it gets through it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;“mellow”&lt;/code&gt; also works for the same reasons above. At the last door &lt;code&gt;w?&lt;/code&gt; it uses its 1 &lt;code&gt;“w”&lt;/code&gt; and goes through it. Yay!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anyway. I think I had way too much fun painting it in more vivid colors. I hope it helped.&lt;/p&gt;

</description>
      <category>regex</category>
      <category>eli5</category>
      <category>nfa</category>
    </item>
  </channel>
</rss>
