# Please help. The error occurred while evaluating nil.name

**URL:** <https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752>\
**Category:** Rails\
**Created:** [April 24, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752 "2008-04-24T17:03:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 24, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/1 "2008-04-24T17:03:48Z")

</div>

Okay so heres my code which works fine, i’ll explain the problem  
below.

\<% for num in removed\_email\_address@domain.invalid %\>

| 

|
| |
| \<%= @results[1].name%\> | \<%= number\_to\_currency(@results[1].price)%\> |
| \<%= link\_to image\_tag(@results[1].image\_url, :size =\> '100x100', :border =\> "0")%\> |
| \<%= h(truncate(@results[1].description, 150))%\> |

 | |

\<%end%\>

This works fine, however when i replace the 1’s with num, which  
should work. i get the error.

“You have a nil object when you didn’t expect it! The error occurred  
while evaluating nil.name.”

I know that num is outputting a number because if i throw \<%=num%\>  
somewhere in the loop it will output all the numbers. I’m stuck on  
this i’ve tried everything i can think of, I know there are other ways  
to loop through this but i need this way to work. Any help would be  
appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 24, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/2 "2008-04-24T17:05:00Z")

</div>

On 24 Apr 2008, at 15:51, nameth wrote:

> Okay so heres my code which works fine, i’ll explain the problem  
> below.
> 
> \<% for num in removed\_email\_address@domain.invalid %\>  
> array indices from from 0 to length -1, not from 1 to length

Fred

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 24, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/3 "2008-04-24T17:17:20Z")

</div>

On Apr 24, 10:04 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Fred

will you marry me

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 24, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/4 "2008-04-24T18:55:01Z")

</div>

Yeah i know i could just use \<%for each prod in @results%\> and then  
reference by \<%=prod.name%\> \<%=prod.price%\> etc… , but the thing is  
i don’t want to “do” what i’m doing for “each” thing in the @results  
collection. here is my finished page, you can probably see why i  
couldn’t do it that way.

 \<% for prods in 0..@stop %\> \<%for i in 0..3%\> \<%end%\> \<%end%\>

| 

| \<%= @results[i].name%\>| \<%= number\_to\_currency(@results[i].price)%\> |
| \<%= link\_to image\_tag(@results[i].image\_url, :size =\> '100x100', :border =\> "0")%\> |
| \<%= h(truncate(@results[i].description, 150))%\> |

 |

\<% for whatsleft in 0..0%\> \<%newstop = (@results.length) - 1%\> \<%for i in 0..@whatsLeft - 1%\> \<%end%\> 

| 

| \<%= @results[newstop].name%\>| \<%= number\_to\_currency(@results[newstop].price)%\> |
| \<%= link\_to image\_tag(@results[newstop].image\_url, :size =\> '100x100', :border =\> "0")%\> |
| \<%= h(truncate(@results[newstop].description, 150))%\> |

 \<%newstop -= 1%\> \<%end%\> |

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 24, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/5 "2008-04-24T18:44:50Z")

</div>

There’s a better way to use the @results collection, too. Some people  
prefer the vb/c# like iteration:

for result in @results do

Personally I don’t like that because it’s one more indirection than  
necessary. I prefer the fundamental each operator:

@results.each do |result|

In either case, you can use all the code you’ve got, but replace all  
the @results[num] references with result (or something more domain  
specific like product…). For example,

\<% @results.each do |product| %\>  
…

\<%= product.name%\> | \<%= number\_to\_currency(product.price)%\>

This version expresses your intentions much more clearly, which will  
make it easier to maintain in the future…

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 25, 2008, 3:53am UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/6 "2008-04-25T03:53:13Z")

</div>

nameth wrote:

> > > \<% for num in 1…

Next, never use ‘for … in’ in Ruby. Matz added it just to look like  
lessor  
languages; to occupy the third page of a Ruby tutorial.

Use names.each do |name| instead.

And BTW everyone knows that online romances never last…

–  
Phlip  
[http://c2.com/cgi/wiki?AssertYinYang](http://c2.com/cgi/wiki?AssertYinYang)

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [May 1, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/7 "2008-05-01T18:51:06Z")

</div>

Thanks Barney, Exactly what i was looking for.

---

<div class="post-metadata">

**Author:** ![nameth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/eada6e/32.png) [@nameth](https://www.ruby-forum.com/u/nameth)\
**Post date:** [April 26, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/please-help-the-error-occurred-while-evaluating-nil-name/135752/8 "2008-04-26T13:05:48Z")

</div>

nameth,

You don’t have to resort to old-school indexed iteration to solve this  
type of problem. Have a look at Rails’ ‘grouping’ extentions to Array:

[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html)

For example:

 \<% @results.in\_groups\_of(4).compact do |result\_group| %\> \<% result\_group.each do |product| %\> \<% end %\> \<% result\_group.each do |product| %\> \<% end %\> \<% result\_group.each do |product| %\> \<% end %\> \<% end %\>

| \<%= h product.name %\>| \<%= number\_to\_currency product.price %\> |
| \<%= link\_to image\_tag(product.image\_url, :size =\> '100x100', :border =\> "0") %\> |
| \<%= h(truncate(product.description, 150)) %\> |

Should produce similar output, but with DRYer, cleaner code.

- Barney
