# Loops in Ruby

**URL:** <https://www.ruby-forum.com/t/loops-in-ruby/180874>\
**Category:** Rails\
**Created:** [December 30, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874 "2009-12-30T23:39:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [December 30, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/1 "2009-12-30T23:39:58Z")

</div>

Hi,

I have a very simple problem. I am a newbie to rails and for the life  
of me can’t figure out why a simple incrementing loop is not printing  
to screen correctly. I know this has to be something simple but can’t  
find anything specific to my problem.

\<%=  
for i in 0…10 do  
puts “i is now #{i}”  
end  
%\>

Prints to screen “0…10” as opposed to what I want:

0  
1  
2  
3  
4  
5  
6  
7  
8  
9  
10

Can someone help me with this very simple problem. I have tried  
until, .times, and while loops to no avail. So there must be some type  
of syntax magic I am missing.

Thanks in advance,  
Justin

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [December 30, 2009, 11:44pm UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/2 "2009-12-30T23:44:04Z")

</div>

On Dec 30, 6:39 pm, webbieguy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> %\>

Because in a view you don’t use puts (puts either goes nowhere or to a  
logfile somewhere).  
The \<%= stuff the result of the chunk of code into the view, for a  
‘for’ statement like you’ve got there then the result is the thing  
iterated over (ie 0…10).

you want something more like

\<% looping construct of your choice do %\>  
i is now \<%= i %\>  
\<% end %\>

Fred

> 8  
> 9  
> 10
> 
> Can someone help me with this very simple problem. I have tried  
> until, .times, and while loops to no avail. So there must be some type  
> of syntax magic I am missing.
> 
> Thanks in advance,  
> Justin

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [December 30, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/3 "2009-12-30T23:54:02Z")

</div>

\<% 0.upto(10) do |i| %\>  
i is now \<%= i %\>  
\<% end %\>

On Dec 30, 1:39 pm, webbieguy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> 6  
> Justin  
> –

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [December 30, 2009, 11:58pm UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/4 "2009-12-30T23:58:43Z")

</div>

On Dec 30, 2009, at 4:08 PM, Frederick C. wrote:

> > puts “i is now #{i}”
> 
> \<% looping construct of your choice do %\>  
> i is now \<%= i %\>  
> \<% end %\>
> 
> Fred

In addition to what Fred says, your other problem is that you have:  
for i in do  
#stuff  
end  
and you want: (note there’s no ‘do’ on a for loop)  
for i in   
#stuff  
end  
so that would be:  
for i in 0…10  
#stuff  
end

You’re getting “0…10” because that’s the value of the expression in  
the \<%= %\>  
Try this in irb and you’ll see.  
irb\> for i in 0…10  
irb\> puts i  
irb\> end  
0  
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
=\> 0…10  
irb\>

It’s more likely, you want to have:

\<% (0…10).each do |i| %\>  
i is now \<%= i %\>  
\<% end %\>

The difference being mainly that the local variable i will only exist  
inside the block and iterating over a collection using .each is much  
more common. See James G.'s article “Shades of Gray: The Evils of  
the For Loop” for a more thorough explanation.

[http://blog.grayproductions.net/articles/the\_evils\_of\_the\_for\_loop](http://blog.grayproductions.net/articles/the_evils_of_the_for_loop)

-Rob

> > 8

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

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [January 1, 2010, 4:31am UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/5 "2010-01-01T04:31:14Z")

</div>

Thank you! I figured it was just a simple thing I was doing wrong.  
Much appreciation!!

Justin

On Dec 30, 4:23 pm, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have a very simple problem. I am a newbie to rails and for the life  
> > Prints to screen “0…10” as opposed to what I want:  
> > 9  
> > 10
> 
> > Can someone help me with this very simple problem. I have tried  
> > until, .times, and while loops to no avail. So there must be some type  
> > of syntax magic I am missing.
> 
> > Thanks in advance,  
> > Justin

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![webbieguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@webbieguy](https://www.ruby-forum.com/u/webbieguy)\
**Post date:** [January 1, 2010, 4:55am UTC](https://www.ruby-forum.com/t/loops-in-ruby/180874/6 "2010-01-01T04:55:15Z")

</div>

On Wed, Dec 30, 2009 at 2:16 PM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > for i in 0…10 do  
> > > you want something more like  
> > > end

Actually, in the above, the ‘do’ is optional for both ‘while’, 'until,  
and  
‘for’ loops  
in both Ruby 1.8.x and 1.9.x. Thus, the above is legal Ruby.  
Furthermore,  
one can write the above using a colon as follows in Ruby 1.8.x:

for i in :  
#stuff  
end

-Conrad

> the \<%= %\>  
> 6  
> \<% (0…10).each do |i| %\>  
> -Rob
> 
> > > 7  
> > > Justin  
> > > For more options, visit this group at  
> > > –

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
