<?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: YenTing Chen</title>
    <description>The latest articles on DEV Community by YenTing Chen (@jimytc).</description>
    <link>https://dev.to/jimytc</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%2F73401%2F7fa68497-741a-42df-bec9-51cf8a630da7.jpg</url>
      <title>DEV Community: YenTing Chen</title>
      <link>https://dev.to/jimytc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jimytc"/>
    <language>en</language>
    <item>
      <title>Trapped by Ruby Operators Precedence</title>
      <dc:creator>YenTing Chen</dc:creator>
      <pubDate>Sat, 22 Feb 2020 16:40:23 +0000</pubDate>
      <link>https://dev.to/jimytc/trapped-by-ruby-operators-precedence-1fk8</link>
      <guid>https://dev.to/jimytc/trapped-by-ruby-operators-precedence-1fk8</guid>
      <description>&lt;h2&gt;
  
  
  Let the story begin
&lt;/h2&gt;

&lt;p&gt;I started to work on a new production code repository, and I could tell my productivity degraded for almost 7 workdays. But this was not because of my lack of domain knowledge in the new production   code nor the new development environment and configuration. Even though I had some issues and was unfamiliar, I still deliver several items.&lt;/p&gt;

&lt;p&gt;It was the inability to do development and run test all in my most familiar IDE. To workaround this, I had to go back and forth between my IDE and terminal to run my test command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observations and the fix
&lt;/h2&gt;

&lt;p&gt;I got two from my investigation. The first one was obvious =&amp;gt; the error. It was an unexpected outgoing network request when running test with my IDE. (We don't want test has dependencies on network.) The other was the different commands for tests. In terminal, I used &lt;code&gt;bundle exec rails test &amp;lt;test_file&amp;gt;&lt;/code&gt; and my IDE uses &lt;code&gt;ruby -Itest &amp;lt;test_file&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So simple thing first, could I reproduce it with the same command from my IDE? And the answer was yes. Great, I got consistency in both environments. Different commands may lead to the differences in the flow of the commands. But let me go into the error one which is more likely a human error.&lt;/p&gt;

&lt;p&gt;Guess what. It's caused by a network library initialization wrapped by a guard condition. Like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="no"&gt;NET_TOOL&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&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;test?&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;= The guard does not work&lt;/span&gt;
  &lt;span class="c1"&gt;# DO the initialization&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Changing it to the code below fixed the problem!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&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;test?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;defined?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NET_TOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;# DO the initialization&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Boom!!
&lt;/h3&gt;

&lt;h2&gt;
  
  
  What happened?
&lt;/h2&gt;

&lt;p&gt;This is something about Ruby's operator precedence. You can refer the post &lt;a href="https://ruby-doc.org/core-2.7.0/doc/syntax/precedence_rdoc.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The original guard condition is,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="no"&gt;NET_TOOL&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&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;test?&lt;/span&gt;&lt;span class="sb"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With which we want it to be.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;defined?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NET_TOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&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;test?&lt;/span&gt;&lt;span class="sb"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator has higher order than &lt;code&gt;defined?&lt;/code&gt;, so the expression actually becomes &lt;code&gt;defined?(NET_TOOL &amp;amp;&amp;amp; !Rails.env.test?)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Further, &lt;code&gt;NET_TOOL&lt;/code&gt; namespace exists and &lt;code&gt;!Rails.env.test?&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; in testing. The code will be recognized as &lt;code&gt;defined?(false)&lt;/code&gt; and it returns a &lt;code&gt;"expression"&lt;/code&gt; string.&lt;/p&gt;

&lt;p&gt;In Ruby, if-statement cares about falsy and truthy, not the boolean value, &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
Truthy means values that are not &lt;code&gt;nil&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. Therefore, our original if-statement always gets a truthy value. And as a result, it always does the initialization.&lt;/p&gt;

&lt;p&gt;Pretty interesting, huh?&lt;/p&gt;

&lt;h2&gt;
  
  
  Thoughts
&lt;/h2&gt;

&lt;p&gt;I believe that the entire effort would never be reduced. It's just moved to somewhere else. Like my story, Ruby allows us to ignore parenthesis but that also makes expression evaluation implicit. To make the program runs as we think, we have to be aware of those implicit rules.&lt;/p&gt;

&lt;p&gt;Maybe there are some better pattern to live with no-parenthesis style. If you know, please let me know. I really want to learn that. But before that, I would suggest using parenthesis to reduce the chance that the code runs differently from we think.&lt;/p&gt;

&lt;h3&gt;
  
  
  Another mysterious thing to dive into but not this time.
&lt;/h3&gt;

&lt;p&gt;Remember the two different commands? That would be another story about what &lt;code&gt;rails&lt;/code&gt; does and how it makes different from &lt;code&gt;ruby&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Originally post on &lt;a href="https://jimytc.com/posts/2020/02/22/trapped_by_ruby_operators_precedence/"&gt;https://jimytc.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>troubleshooting</category>
      <category>operatorsprecedence</category>
    </item>
  </channel>
</rss>
