<?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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F234156%2F88af195e-c752-49a6-9f6f-d3939670b4e0.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 - translation_missing class style in development</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 every missing &lt;code&gt;i18n&lt;/code&gt; key in your app. Rails adds a &lt;code&gt;&amp;lt;span class="translation_missing"&amp;gt;&amp;lt;/span&amp;gt;&lt;/code&gt; around every use of the &lt;code&gt;t&lt;/code&gt; helper that does not have a matching key in the YAML for the current locale.&lt;/p&gt;

&lt;p&gt;To help the team spot those, we recently started using a simple CSS rule:&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 missing key:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd4ngahv3jm2pal6v5ho1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd4ngahv3jm2pal6v5ho1.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 outlines in production! 😱&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most of the solutions I found on Stack Overflow were either outdated or too complex to trust.&lt;/p&gt;

&lt;p&gt;I decided to read how Rails creates those span classes and look for a fix. After my third monkey patch to Rails’ &lt;code&gt;ActionView&lt;/code&gt;, I realized 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; tag:&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;Regex engines often talk about NFAs: &lt;strong&gt;nondeterministic finite automata&lt;/strong&gt;. That name is a mouthful, so here is the plain idea. An NFA is a little machine with a finite set of states. It reads input one character at a time and moves from state to state. “Nondeterministic” means that at some points more than one move is legal, or a move can fail and the machine still has other attempts to try. A lot of how a regex “tries” a pattern against a string is that kind of walk: advance when a piece matches, and when it does not, back up and try from another place.&lt;/p&gt;

&lt;p&gt;The rest of this piece is one concrete, slightly silly picture of that walk.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Automaton
&lt;/h2&gt;

&lt;p&gt;You have a robot (our automaton) that walks the REGEX dungeon door by door. Each door is one piece of the pattern. The string is the belt of keys the robot carries, used in order, one character at a time.&lt;/p&gt;

&lt;p&gt;To open the next door, the robot must spend the next key (or keys) that that door accepts. If it reaches the last door and gets through, the pattern matches. If it hits a key that cannot open the current door, it fails that attempt: it is sent back to the first door of the pattern and starts again from the &lt;em&gt;next&lt;/em&gt; character in the string. Progress on the previous attempt does not carry over.&lt;/p&gt;

&lt;p&gt;So with the string &lt;code&gt;"budega"&lt;/code&gt;, if an attempt fails on &lt;code&gt;"b"&lt;/code&gt;, the next attempt starts with &lt;code&gt;"udega"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Literal doors
&lt;/h2&gt;

&lt;p&gt;For the dungeon &lt;code&gt;/there/&lt;/code&gt;, there are five doors in a row: &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;. Each needs exactly that key, in that order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;"there"&lt;/code&gt; works. The robot uses every key and exits the dungeon.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"here"&lt;/code&gt; never opens the first door. It tries &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 leaves sad.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"then go over there"&lt;/code&gt; looks promising at first. The robot opens &lt;code&gt;t&lt;/code&gt;, &lt;code&gt;h&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, then stalls on &lt;code&gt;"n"&lt;/code&gt; because the next door wants &lt;code&gt;r&lt;/code&gt;. That attempt dies. It restarts from &lt;code&gt;"hen go over there"&lt;/code&gt;, fails the first door over and over, until it finally tries from &lt;code&gt;"there"&lt;/code&gt; and gets through.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Doors that accept more than one key shape
&lt;/h2&gt;

&lt;p&gt;Some doors take a choice of characters (ranges) or a flexible count (quantifiers).&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;/[hm]el+ow?/&lt;/code&gt;, the five doors are: &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; matches. The first door accepts &lt;code&gt;h&lt;/code&gt; or &lt;code&gt;m&lt;/code&gt;. The &lt;code&gt;l+&lt;/code&gt; door takes both &lt;code&gt;l&lt;/code&gt;s. The &lt;code&gt;o&lt;/code&gt; door takes &lt;code&gt;o&lt;/code&gt;. The last door &lt;code&gt;w?&lt;/code&gt; accepts zero or one &lt;code&gt;w&lt;/code&gt;; here it takes zero and the robot still passes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"mellow"&lt;/code&gt; matches for the same reasons. At &lt;code&gt;w?&lt;/code&gt; the robot spends its one &lt;code&gt;w&lt;/code&gt; and walks out.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had a lot of fun painting this in bright colors. I hope the map still helps when the metaphor gets loud.&lt;/p&gt;

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