# Is there something I am missing?

**URL:** <https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008>\
**Category:** Rails\
**Created:** [April 26, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008 "2007-04-26T15:35:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [April 26, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008/1 "2007-04-26T15:35:11Z")

</div>

this s more of a syntax question then a problem persay. I was optimizing  
some older code on one of my projects when I hit and interesting  
problem. First the Working Code:

placeholder = 0  
while(@record\_album[placeholder])  
temp =@record\_album[placeholder]  
if(temp.parentid == Parent.id)  
album\_tree +="\t

- \<a  
href="#"\>#{@record[placeholder][“name”]}
\n"  
placeholder+=1  
end  
end

Now Non working Code:

placeholder = 0  
while(@record\_album[placeholder].parentid == Parent.id)  
album\_tree +="\t

- \<a  
href="#"\>#{@record\_album[placeholder][“name”]}
\n"  
placeholder+=1  
end

The non working code gives me a “You have a nil object when you didn’t  
expect it! The error occurred while evaluating nil.parentid” exception.  
I am hoping this is something stupid on my part, but if it isn’t then  
why does the error occur

Darushin

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [April 26, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008/2 "2007-04-26T16:24:44Z")

</div>

Hi Darushin,

Darushin wrote:

> First the Working Code:
> 
> placeholder = 0  
> while(@record\_album[placeholder])

From Pickaxe, p93, “Ruby has a simple definition of truth. Any value  
that  
is not nil or the constant false is true.” The while loop executes  
until  
@record\_album[placeholder] evaluates to nil.

> while(@record\_album[placeholder].parentid == Parent.id)  
> gives me a “You have a nil object when you didn’t  
> expect it! The error occurred while evaluating nil.parentid”

This error occurs because Ruby does not allow you to invoke a method on  
a  
nil object. Your working code avoids this by performing the test for a  
nil  
object prior to attempting to invoke the .parentid method.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [April 26, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008/3 "2007-04-26T16:52:16Z")

</div>

Thank you so much. I figured there was a reason for this behavior and  
it was driving me nuts to figure out why it was doing this.

Ben

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [April 26, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008/4 "2007-04-26T17:03:51Z")

</div>

Hi Ben,

Darushin wrote:

> Thank you so much. I figured there was a reason  
> for this behavior and it was driving me nuts to figure  
> out why it was doing this.

You’re welcome. Glad to help.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [April 27, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/is-there-something-i-am-missing/93008/5 "2007-04-27T17:16:31Z")

</div>

> placeholder = 0  
> while(@record\_album[placeholder])  
> temp =@record\_album[placeholder]  
> if(temp.parentid == Parent.id)  
> album\_tree +="\t
> 
> - \<a  
> href="#"\>#{@record[placeholder][“name”]}
> \n"  
> placeholder+=1  
> end  
> end

Ruby has lots of great iterators which can almost always be used in  
place of such iteration schemes using increments:

@record\_album.compact.each do |album|  
next unless album.parentid == Parent.id  
do stuff  
end

Array#compact will remove any nil entries from your @record\_album  
array (I assume it’s an array). Note that the behavior of this one  
would be different from the original in that the original loop breaks  
on the first element that is nil or false and this one will simply  
skip it and continue.

Cheers,  
Rein
