Loops in Ruby

3 views
Skip to first unread message

webbieguy

unread,
Dec 30, 2009, 1:39:28 PM12/30/09
to Ruby on Rails: Talk
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

Frederick Cheung

unread,
Dec 30, 2009, 4:08:54 PM12/30/09
to Ruby on Rails: Talk

On Dec 30, 6:39 pm, webbieguy <justin.dean.griff...@gmail.com> wrote:
> 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
> %>
>

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

Rob Biedenharn

unread,
Dec 30, 2009, 5:16:59 PM12/30/09
to rubyonra...@googlegroups.com
On Dec 30, 2009, at 4:08 PM, Frederick Cheung wrote:
> On Dec 30, 6:39 pm, webbieguy <justin.dean.griff...@gmail.com> wrote:
>> 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
>> %>
>>
> 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

In addition to what Fred says, your other problem is that you have:
for i in <Range> do
#stuff
end
and you want: (note there's no 'do' on a for loop)
for i in <Range>
#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 Gray'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

-Rob

>> 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 rubyonrails-
> ta...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>
>

Rob Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com

David

unread,
Dec 30, 2009, 5:23:57 PM12/30/09
to Ruby on Rails: Talk
<% 0.upto(10) do |i| %>

i is now <%= i %>
<% end %>

webbieguy

unread,
Dec 31, 2009, 10:29:40 PM12/31/09
to Ruby on Rails: Talk

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

Justin

Conrad Taylor

unread,
Dec 31, 2009, 10:53:32 PM12/31/09
to rubyonra...@googlegroups.com
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 <Range> :
  #stuff
end

-Conrad
 
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages