# Iterating through an Instance Variable in my View

**URL:** <https://www.ruby-forum.com/t/iterating-through-an-instance-variable-in-my-view/182041>\
**Category:** Rails\
**Created:** [January 21, 2010, 10:48pm UTC](https://www.ruby-forum.com/t/iterating-through-an-instance-variable-in-my-view/182041 "2010-01-21T22:48:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 21, 2010, 10:48pm UTC](https://www.ruby-forum.com/t/iterating-through-an-instance-variable-in-my-view/182041/1 "2010-01-21T22:48:43Z")

</div>

I have an instance variable called @ls which is an array that contains a  
list of files in a directory.

When I call this instance variable in my view like this:

\<%= @ls %\>

It shows the entire list of files in the array.

Since I need to edit what is displayed for each file name in the array,  
I want to iterate through it in my view.

When I put this code in my view:  
\<% @ls.each do |a| %\>  
a.puts  
\<% end %\>

… it only prints out the letter “a” as many times as the size of the  
array.

Can anyone help me out with correcting this issue?

thanks

jackster

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 21, 2010, 11:27pm UTC](https://www.ruby-forum.com/t/iterating-through-an-instance-variable-in-my-view/182041/2 "2010-01-21T23:27:29Z")

</div>

On Jan 21, 2010, at 4:48 PM, jackster the jackle wrote:

> I have an instance variable called @ls which is an array that  
> contains a  
> list of files in a directory.
> 
> When I call this instance variable in my view like this:
> 
> \<%= @ls %\>
> 
> It shows the entire list of files in the array.

Because there’s an implicit to\_s on the contents of \<%= %\>

> array.
> 
> Can anyone help me out with correcting this issue?
> 
> thanks
> 
> jackster

You probably want them in some sort of list, right?

 \<% @ls.each do |a| %\> - \<%= a %\> 
 \<% end %\>

Replace the ul/li markup with whatever makes sense for your results.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
