<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag gotchas</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/gotchas</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Ahh, the Subtleties of Ruby's Operator Precedence</title>
      <description>&lt;p&gt;&lt;img src="http://talklikeaduck.denhaven2.com/files/precedence.png" class="tease-image"/&gt;
&lt;p&gt;Not long ago while working on an existing Rails application, (i.e. code I hadn&amp;#8217;t written). I was mystified 
when a logical expression seemed to be returning an odd result.  The expression had been written with &lt;strong&gt;not&lt;/strong&gt;,
&lt;strong&gt;and&lt;/strong&gt;, and &lt;strong&gt;or&lt;/strong&gt;, and was the right hand side of an assignment statement.  ventually I changed to using &lt;strong&gt;!&lt;/strong&gt;,
&lt;strong&gt;&amp;#38;&amp;#38;&lt;/strong&gt;, and &lt;strong&gt;||&lt;/strong&gt; which fixed the problem. 
I never completely
understood what was going on, until I encountered this 
&lt;a href="http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html"&gt;blog entry by Jay Fields.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the big differences between Ruby and Smalltalk is the use of operator precedence.  In Smalltalk, the grammar is
very simple, unary message selectors bind tighter than binary selectors, which bind tighter than keyword selectors.
The assignment operator binds more loosely than any of these.  This is often a stumbling block for newcomers to Smalltalk
when they find that in Smalltalk 3 + 5 * 2 evaluates to 16.&lt;/p&gt;
&lt;p&gt;Ruby in comparison has quite a rich and flexible syntax.
Usually, it&amp;#8217;s quite intutitive, but, depending on &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/06/21/where-i-come-from"&gt;where you come from&lt;/a&gt; there can be some surprises.&lt;/p&gt;
&lt;p&gt;I understood the relative precendences of, say &lt;strong&gt;&amp;#38;&amp;#38;&lt;/strong&gt; vs. &lt;strong&gt;and&lt;/strong&gt; in Ruby, but the fact that 
&lt;strong&gt;and&lt;/strong&gt; had weaker precedence than &lt;strong&gt;=&lt;/strong&gt; escaped me, until Jay&amp;#8217;s article turned the light on for
me.&lt;/p&gt;
&lt;p&gt;As they say, you learn something new everyday, or at least you hope you do.  I don&amp;#8217;t expect that I&amp;#8217;ll forget this little
corner of Ruby in future&lt;/p&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 07 Aug 2007 15:24:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0f8801a7-807d-4bfb-9686-ef64b0eb3a05</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/08/07/ahh-the-subtleties-of-rubys-operator-precedence</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>gotchas</category>
      <category>syntax</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/452</trackback:ping>
    </item>
    <item>
      <title>Qui ipso probo probatur?</title>
      <description>&lt;p&gt;The other day I was working on adding support for user selected time-zones to an existing rails app for a client.  As usual I was doing test-first development. One of the things that makes rails such a pleasure is that a good set of tests give confidence that you aren&amp;#8217;t breaking &amp;#8220;legacy&amp;#8221; code.&lt;/p&gt;
&lt;p&gt;
I also use, and really like, the &lt;a href="http://www.vim.org/scripts/script.php?script_id=1567"&gt;rails plugin for vim&lt;/a&gt; which does lots of nice things like making navigation between the files of rails apps much easier.  It also has a nice feature which adds a :Rake command to vim which &amp;#8220;does the right&amp;#8221; thing contextually.  For example if you are editing a migration and enter :Rake, it runs rake db:migrate.  If you are in a test file it runs just the single test selected by the cursor, or just that test file if you aren&amp;#8217;t positioned to a particular test.&lt;/p&gt;
&lt;p&gt;
I was doing the latter, and my test was failing, and I was having a hard time debugging it.  I tried executing just that single test from the bash command line with:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ruby test/unit/my_test.rb -n&amp;quot;test_mytest&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
and it still failed, no surprise.  The same thing happened if I ran the entire test file, in fact, other tests which had worked before were now failing, and I was really mystified now because I didn&amp;#8217;t see how I had done anything which had a remote chance of breaking those.
&lt;p&gt;So I figured I probably should test everthing so:&lt;/p&gt;
&lt;typo:code&gt;
$rake test
&lt;/typo&gt;
&lt;p&gt;And, surprise of surprises, but &lt;strong&gt;every test worked&lt;/strong&gt;. Not only my failing unit tests, but the functional and integration tests as well.&lt;/p&gt;
&lt;p&gt;To make a long story short, the problem turned out to be that the tests were failing because, now that the particular model was sensitive to the user&amp;#8217;s timezone, it needed access to it&amp;#8217;s associated user model, and I hadn&amp;#8217;t told the testcase that the users fixture was needed.  Running the testcase in isolation, the users table wasn&amp;#8217;t being populated for the test case.  But running rake test, or rake test:units meant that other tests run before had left the data I needed behind.&lt;/p&gt;
&lt;p&gt;Just a little thing that makes fixtures less than ideal.&lt;/p&gt;</description>
      <pubDate>Sat, 14 Jul 2007 15:28:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:503f95d2-19dd-4bdf-b3be-a2c730e6c9c8</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/07/14/qui-ipso-probo-probatur</link>
      <category>ruby</category>
      <category>rails</category>
      <category>testing</category>
      <category>fixtures</category>
      <category>gotchas</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/441</trackback:ping>
    </item>
  </channel>
</rss>
