# Need help for loop

**URL:** <https://www.ruby-forum.com/t/need-help-for-loop/125572>\
**Category:** Rails\
**Created:** [January 10, 2008, 9:30am UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572 "2008-01-10T09:30:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 9:30am UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/1 "2008-01-10T09:30:35Z")

</div>

Hi,  
Can anyone please help me. What I want to do here is to print the label  
and text field of this part of the table 5 times. The output I expected  
to get is this:

TEXT NUMBER 0 (this is where the TEXT\_FIELD will appear)  
TEXT NUMBER 1 (this is where the TEXT\_FIELD will appear)  
.  
.  
.  
TEXT NUMBER 5 (this is where the TEXT\_FIELD will appear)

This are the codes I used:

\<% for num in (0…4) %\>

  
TEXT NUMBER “#{num}”  
  
  
  
\<% end %\>

Thanks,  
fries88

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 10:12am UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/2 "2008-01-10T10:12:01Z")

</div>

Hi,  
Can anyone please help me. What I need to do here is to print the label  
and text field of this part of the table 5 times. The output I expected  
to get is this:

TEXT NUMBER 0 (this is where the TEXT\_FIELD will appear)  
TEXT NUMBER 1 (this is where the TEXT\_FIELD will appear)  
.  
.  
.  
TEXT NUMBER 5 (this is where the TEXT\_FIELD will appear)

Instead, what I get is:

TEXT NUMBER #{num} (this is where the TEXT\_FIELD will appear)  
TEXT NUMBER #{num} (this is where the TEXT\_FIELD will appear)  
.  
.  
.  
TEXT NUMBER #{num} (this is where the TEXT\_FIELD will appear)

This are the codes I used:

\<% for num in (0…4) %\>

  
TEXT NUMBER “#{num}”  
  
  
  
\<% end %\>

Thanks,  
fries88

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 10:51am UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/3 "2008-01-10T10:51:00Z")

</div>

August L. wrote:

> Can you loop in Ruby?
> 
> Also, you need to use erb to execute the code. I bet you don’t  
> understand what that means.
> 
> On Jan 10, 10:12?am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi,

Yup I don’t understand what your talking about. Can anyone please  
explain?

Thanks,  
user splash (I am a beginner)

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/4 "2008-01-10T12:54:37Z")

</div>

On 10 Jan 2008, at 09:51, user splash wrote:

> Hi,
> 
> Yup I don’t understand what your talking about. Can anyone please  
> explain?
> 
> Thanks,  
> user splash (I am a beginner)

It’s rather confusing if you post with 2 different identities (seems  
odd that both you and fries88 have exactly the same question).  
Anyway, go back to where you first read about rhtml templates and read  
about the difference between \<% and \<%=  
#{} only works when ruby evaluates a string, which isn’t what is  
happening here.

Fred

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/5 "2008-01-10T15:19:56Z")

</div>

Hi um fries 88,

I think the only problem is the TEXT NUMBER “#{num}” line. Where  
you’ve got #{num} here isn’t being interpreted by ruby because you’ve  
not wrapped it with an \<% %\> to tell ruby to take a look at it.  
Really you don’t need the “#{num}” all you should need to do is TEXT  
NUMBER \<%= num %\>.

Just fyi, using a for loop as you are is a little slower then using  
either the times or upto… as far as why it is slower I’m not really  
sure, but times and upto are definitely the “ruby way”…

e.g.

\<% 5.times do |num| %\>  
… TEXT NUMBER \<%= num %\> …  
\<% end %\>

or

\<% 0.upto 4 do |num| %\>  
… TEXT NUMBER \<%= num %\> …  
\<% end %\>

The upto form has the added benefit that you can start from an  
arbitrary place (like 1 if you wanted to be 1 based instead of zero  
based) and go through what your ended number should be.

\<% 1.upto 5 do |num| %\>  
…  
\<% end %\>

Hope that helps.  
-andy:)

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 10, 2008, 10:28am UTC](https://www.ruby-forum.com/t/need-help-for-loop/125572/6 "2008-01-10T10:28:01Z")

</div>

Can you loop in Ruby?

Also, you need to use erb to execute the code. I bet you don’t  
understand what that means.

On Jan 10, 10:12 am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
