<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Check for nil and initialize on a single line</title>
	<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/</link>
	<description>Because programming should be fun</description>
	<pubDate>Tue, 06 Jan 2009 03:44:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Arne</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-61</link>
		<author>Arne</author>
		<pubDate>Wed, 04 Apr 2007 14:31:29 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-61</guid>
					<description>I always forget there's a nil? message, usually I do the &#124;&#124;=.

Just curious, what's a fleebie?</description>
		<content:encoded><![CDATA[<p>I always forget there&#8217;s a nil? message, usually I do the ||=.</p>
<p>Just curious, what&#8217;s a fleebie?</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-63</link>
		<author>Frank</author>
		<pubDate>Wed, 04 Apr 2007 17:00:28 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-63</guid>
					<description>Fleebie is nothing really. I just thought that it would be memorable. =)

There was a squirrel in some weird humor show. I don't recall how they called it exactly, but it somewhat sounded like "ruby fleebie". 

I would have prefered to tell you another reason...</description>
		<content:encoded><![CDATA[<p>Fleebie is nothing really. I just thought that it would be memorable. =)</p>
<p>There was a squirrel in some weird humor show. I don&#8217;t recall how they called it exactly, but it somewhat sounded like &#8220;ruby fleebie&#8221;. </p>
<p>I would have prefered to tell you another reason&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: zerohalo</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-65</link>
		<author>zerohalo</author>
		<pubDate>Wed, 04 Apr 2007 17:57:16 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-65</guid>
					<description>Something that I find odd about the nil? method is the differentiation between an object being nil and the non-existence of an object. They are not the same. Consider:

&#62;&#62; puts x = 1 if x.nil?
=&#62; 1
&#62;&#62; puts 'z is not defined' if z.nil?
=&#62; NameError: undefined local variable or method 'z'

How is that explained? In the first instance, the nil? method is obviously creating the x object, assigning nil to it, and then assigning the value 1. In the second instance, nil? is not creating the z object and thus returning an error.</description>
		<content:encoded><![CDATA[<p>Something that I find odd about the nil? method is the differentiation between an object being nil and the non-existence of an object. They are not the same. Consider:</p>
<p>&gt;&gt; puts x = 1 if x.nil?<br />
=&gt; 1<br />
&gt;&gt; puts &#8216;z is not defined&#8217; if z.nil?<br />
=&gt; NameError: undefined local variable or method &#8216;z&#8217;</p>
<p>How is that explained? In the first instance, the nil? method is obviously creating the x object, assigning nil to it, and then assigning the value 1. In the second instance, nil? is not creating the z object and thus returning an error.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-66</link>
		<author>Frank</author>
		<pubDate>Wed, 04 Apr 2007 18:28:13 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-66</guid>
					<description>Zerohalo, that's a very good observation.

Part of the answer is because "=" binds higher than the if modifier. So, in your first example, x is assigned before the condition is executed. That's why ruby doesn't complain. 

However, I'm not sure about the rest of the story. I GUESS that a nil object is assigned to x at this exact moment and NOT the fixnum 1. Otherwise the if modifier would always returns false...</description>
		<content:encoded><![CDATA[<p>Zerohalo, that&#8217;s a very good observation.</p>
<p>Part of the answer is because &#8220;=&#8221; binds higher than the if modifier. So, in your first example, x is assigned before the condition is executed. That&#8217;s why ruby doesn&#8217;t complain. </p>
<p>However, I&#8217;m not sure about the rest of the story. I GUESS that a nil object is assigned to x at this exact moment and NOT the fixnum 1. Otherwise the if modifier would always returns false&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: zerohalo</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-68</link>
		<author>zerohalo</author>
		<pubDate>Wed, 04 Apr 2007 21:00:14 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-68</guid>
					<description>Frank, since I was curious,  I posted the question on the Ruby mailing list, and found the answer. In brief, Ruby sees x and figures it must refer to a variable and therefore creates that variable x, assigning nil value to it, before running x.nil? In the second instance, since the first reference to z is z.nil? and therefore Ruby assumes z is a method rather than variable, does nothing, and therefore z.nil? throws and exception.

See here:

http://www.ruby-forum.com/topic/103808</description>
		<content:encoded><![CDATA[<p>Frank, since I was curious,  I posted the question on the Ruby mailing list, and found the answer. In brief, Ruby sees x and figures it must refer to a variable and therefore creates that variable x, assigning nil value to it, before running x.nil? In the second instance, since the first reference to z is z.nil? and therefore Ruby assumes z is a method rather than variable, does nothing, and therefore z.nil? throws and exception.</p>
<p>See here:</p>
<p><a href="http://www.ruby-forum.com/topic/103808" rel="nofollow">http://www.ruby-forum.com/topic/103808</a></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-69</link>
		<author>Frank</author>
		<pubDate>Wed, 04 Apr 2007 21:46:35 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-69</guid>
					<description>Very interesting... I read the post you are relating to and it's not too far away from what I was thinking.</description>
		<content:encoded><![CDATA[<p>Very interesting&#8230; I read the post you are relating to and it&#8217;s not too far away from what I was thinking.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-75</link>
		<author>Chris</author>
		<pubDate>Thu, 05 Apr 2007 01:58:16 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/check-for-nil-and-initialize-on-a-single-line/#comment-75</guid>
					<description>The difference between

1)  x = get_some_object if x.nil?

and

2)  x &#124;&#124;= get_some_object

is that in the first instance x will get reassigned only when it's nil, while in the second it will get reassigned if it's nil or false.

Something to know, just in case.</description>
		<content:encoded><![CDATA[<p>The difference between</p>
<p>1)  x = get_some_object if x.nil?</p>
<p>and</p>
<p>2)  x ||= get_some_object</p>
<p>is that in the first instance x will get reassigned only when it&#8217;s nil, while in the second it will get reassigned if it&#8217;s nil or false.</p>
<p>Something to know, just in case.</p>
]]></content:encoded>
				</item>
</channel>
</rss>
