<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: How Elvis showed me a neat way of using operators in Ruby</title>
	<atom:link href="http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/</link>
	<description>Peter Maas's Weblog</description>
	<pubDate>Sat, 30 Aug 2008 09:37:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: peter</title>
		<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12536</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 22 Dec 2007 21:57:30 +0000</pubDate>
		<guid isPermaLink="false">http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12536</guid>
		<description>@levi yes, those results are as expected... it's Rubys' way of avoiding having booleans with three states.

Your fix works, but personally I would avoid using the ternary operator in Ruby to solve this... I'd use a &lt;em&gt;trailing if&lt;/em&gt; solution:

&lt;pre&gt;
display_icons = true if display_icons.nil?
&lt;/pre&gt;

By the way,  since autoboxing was added to Java this is sort of a problem:

&lt;pre&gt;
Boolean b = null;

if(b){
  System.out.println(b)
} 
&lt;/pre&gt;

This throws a NullPointerException during unboxing; whereas  Ruby or Groovy evaluate the expression correctly (assuming that the uninitialized state of a boolean is false).</description>
		<content:encoded><![CDATA[<p>@levi yes, those results are as expected&#8230; it&#8217;s Rubys&#8217; way of avoiding having booleans with three states.</p>
<p>Your fix works, but personally I would avoid using the ternary operator in Ruby to solve this&#8230; I&#8217;d use a <em>trailing if</em> solution:</p>
<pre>
display_icons = true if display_icons.nil?
</pre>
<p>By the way,  since autoboxing was added to Java this is sort of a problem:</p>
<pre>
Boolean b = null;

if(b){
  System.out.println(b)
}
</pre>
<p>This throws a NullPointerException during unboxing; whereas  Ruby or Groovy evaluate the expression correctly (assuming that the uninitialized state of a boolean is false).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: levi_h</title>
		<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12528</link>
		<dc:creator>levi_h</dc:creator>
		<pubDate>Sat, 22 Dec 2007 10:26:23 +0000</pubDate>
		<guid isPermaLink="false">http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12528</guid>
		<description>&#62;&#62; display_icons &#124;&#124;= true
&#62;&#62; would *always* set display_icons to true, even if it was inited to false.

But that's what you'd expect, right? That the &#124;&#124; (or 'or') operator would return true for true and false? And with that in mind, you'd more likely write something like display_icons = (display_icons.nil? ? true : display_icons).

As I understand it, C# has the same operator, ??, that returns a default value if the lhs is null. It would be nice to have an operator like this in Java as well: it especially pays off with longer expressions, which almost require you to introduce a local variable.</description>
		<content:encoded><![CDATA[<p>&gt;&gt; display_icons ||= true<br />
&gt;&gt; would *always* set display_icons to true, even if it was inited to false.</p>
<p>But that&#8217;s what you&#8217;d expect, right? That the || (or &#8216;or&#8217;) operator would return true for true and false? And with that in mind, you&#8217;d more likely write something like display_icons = (display_icons.nil? ? true : display_icons).</p>
<p>As I understand it, C# has the same operator, ??, that returns a default value if the lhs is null. It would be nice to have an operator like this in Java as well: it especially pays off with longer expressions, which almost require you to introduce a local variable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12494</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Fri, 21 Dec 2007 12:16:24 +0000</pubDate>
		<guid isPermaLink="false">http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12494</guid>
		<description>Thanks Mich, this is a very valid point. The Elvis operator 'suffers' from the same problem:

&lt;pre&gt;
groovy:000&gt; a = false
===&gt; false
groovy:000&gt; c = a ?: true
===&gt; true
&lt;/pre&gt;

And yeah, I like the "Wham!" concept!</description>
		<content:encoded><![CDATA[<p>Thanks Mich, this is a very valid point. The Elvis operator &#8217;suffers&#8217; from the same problem:</p>
<pre>
groovy:000> a = false
===> false
groovy:000> c = a ?: true
===> true
</pre>
<p>And yeah, I like the &#8220;Wham!&#8221; concept!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mich.barsinai</title>
		<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12493</link>
		<dc:creator>mich.barsinai</dc:creator>
		<pubDate>Fri, 21 Dec 2007 12:02:13 +0000</pubDate>
		<guid isPermaLink="false">http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12493</guid>
		<description>I'm afraid that is just another case where ruby tries to be nice and then stabs you in the back unintentionally. There is a very good reason for groovy to go with "?:", and that is values inited to false.

display_icons &#124;&#124;= true 
would *always* set display_icons to true, even if it was inited to false.

Suppose you have a rails application. And suppose that you have a partial which you render with a bunch of locals. One of them might be "display_icons", which you want to be true unless otherwise defined. You put the above snippet at the top of the code (real gurus would probably iterate on the keys and of the hash to do that, and would get bitten much harder). You then render the partial with :locals=&#62;{:display_icons=false}, and you would still get the icons.

Operator overloading is trickier than one might think. There's a reason why it's not in java (see also here: http://cafe.elharo.com/java/operator-overloading/).

BTW, if ?: called Elvis (why?), I propose we call the ruby &#124;&#124; operator "Wham!" (it does look like 2 skinny guys, and it is the sound your forehead would make when hitting the keyboard after you realized why those icons were showing all the time :-).</description>
		<content:encoded><![CDATA[<p>I&#8217;m afraid that is just another case where ruby tries to be nice and then stabs you in the back unintentionally. There is a very good reason for groovy to go with &#8220;?:&#8221;, and that is values inited to false.</p>
<p>display_icons ||= true<br />
would *always* set display_icons to true, even if it was inited to false.</p>
<p>Suppose you have a rails application. And suppose that you have a partial which you render with a bunch of locals. One of them might be &#8220;display_icons&#8221;, which you want to be true unless otherwise defined. You put the above snippet at the top of the code (real gurus would probably iterate on the keys and of the hash to do that, and would get bitten much harder). You then render the partial with :locals=&gt;{:display_icons=false}, and you would still get the icons.</p>
<p>Operator overloading is trickier than one might think. There&#8217;s a reason why it&#8217;s not in java (see also here: <a href="http://cafe.elharo.com/java/operator-overloading/" rel="nofollow">http://cafe.elharo.com/java/operator-overloading/</a>).</p>
<p>BTW, if ?: called Elvis (why?), I propose we call the ruby || operator &#8220;Wham!&#8221; (it does look like 2 skinny guys, and it is the sound your forehead would make when hitting the keyboard after you realized why those icons were showing all the time :-).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: railsguru</title>
		<link>http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12462</link>
		<dc:creator>railsguru</dc:creator>
		<pubDate>Thu, 20 Dec 2007 10:53:37 +0000</pubDate>
		<guid isPermaLink="false">http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/#comment-12462</guid>
		<description>It's very useful to init a variable which might have been initted before, at least that's where I use it most frequently e.g.:

queue &#124;&#124;= []

// now you're sure you have a valid queue array</description>
		<content:encoded><![CDATA[<p>It&#8217;s very useful to init a variable which might have been initted before, at least that&#8217;s where I use it most frequently e.g.:</p>
<p>queue ||= []</p>
<p>// now you&#8217;re sure you have a valid queue array</p>
]]></content:encoded>
	</item>
</channel>
</rss>
