Forum: Ruby on Rails Iterator not iterating

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
597a974d5779de41c5524362bf835b52?d=identicon&s=25 Joshua Willis (mtnbiker98)
on 2016-11-06 23:33
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 "<h2>Hello World!</h2><p>"}%>
The time is now <%= Time.now %>
</p>

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
3e312ef6c09f4106f0f76b83d9dc7602?d=identicon&s=25 Johnny Stewart (j77j)
on 2016-11-08 11:22
Hi Joshua,

what you probably want is something along these lines:

  <% 4.times do |x|%>
      <h2>Hello World!</h2><p>
  </p>
  <% end %>
  The time is now <%= Time.now %>
  </p>

your code uses puts which prints <h2>Hello World!</h2><p> 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 "<h2>Hello World!</h2><p>"}%>
> The time is now <%= Time.now %>
> </p>
>
> 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
This topic is locked and can not be replied to.