Iterator not iterating


#1

Hello,

I will preface this with I am very new to ruby/rails and am working on
teaching myself. I have a rails dev environment on OSX and am seeing
some behavior in rails that I cannot correct. My source from
index.html.erb is below:

<%=4.times {puts “

Hello World!

”}%>
The time is now <%= Time.now %>

The html output of this is

4
The time is now xx:xx:xx:xx

I was expecting

Hello World!
Hello World!
Hello World!
Hello World!
The time is now xx:xx:xx:xx

What am I messing up here?

Thank you for any assistance. I appreciate it.

Josh


#2

Hi Joshua,

what you probably want is something along these lines:

<% 4.times do |x|%>

Hello World!

<% end %> The time is now <%= Time.now %>

your code uses puts which prints

Hello World!

to the console
4 times, so that is why that doesn’t show up in the html. It then
returns the integer you passed in - that is the 4.

change it for the code above and you should see:

Hello World!
Hello World!
Hello World!
Hello World!
The time is now xx:xx:xx:xx

J.

Joshua Willis wrote in post #1185202:

Hello,

I will preface this with I am very new to ruby/rails and am working on
teaching myself. I have a rails dev environment on OSX and am seeing
some behavior in rails that I cannot correct. My source from
index.html.erb is below:

<%=4.times {puts “

Hello World!

”}%>
The time is now <%= Time.now %>

The html output of this is

4
The time is now xx:xx:xx:xx

I was expecting

Hello World!
Hello World!
Hello World!
Hello World!
The time is now xx:xx:xx:xx

What am I messing up here?

Thank you for any assistance. I appreciate it.

Josh