<?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: Ahh, the Subtleties of Ruby's Operator Precedence</title>
    <link>http://talklikeaduck.denhaven2.com/articles/2007/08/07/ahh-the-subtleties-of-rubys-operator-precedence</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>"Ahh, the Subtleties of Ruby's Operator Precedence" by Daniel Berger</title>
      <description>&lt;p&gt;I don&amp;#8217;t think I&amp;#8217;ve ever used &amp;#8216;and&amp;#8217;, &amp;#8216;or&amp;#8217; or &amp;#8216;not&amp;#8217;, and I doubt I ever will precisely because it&amp;#8217;s too easy to get yourself into trouble with precedence.&lt;/p&gt;</description>
      <pubDate>Wed, 08 Aug 2007 06:05:24 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0dd7120f-62d4-4a76-89d2-29569bb55d57</guid>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/08/07/ahh-the-subtleties-of-rubys-operator-precedence#comment-567</link>
    </item>
  </channel>
</rss>
