Forum: Ruby on Rails RE: templates and arrays

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.
F01c3ff32fee35d911e81c172a005012?d=identicon&s=25 Tom Fakes (Guest)
on 2005-12-13 06:09
(Received via mailing list)
The best way to do this is:

<% products.each_with_index { |product, index| %>
	<%= product.size %>
<% } %>

You should forget *for*, it is not needed in Ruby Land.  Enumerations
such as *each* are more efficient when you get to more interesting cases
- check out the documentation for Enumerable
(http://ruby-doc.org/core/classes/Enumerable.html) and Array
(http://ruby-doc.org/core/classes/Array.html)

Forget the For loop - it's so 1998!
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2005-12-14 15:33
(Received via mailing list)
Tom Fakes wrote:
> The best way to do this is:
>
> <% products.each_with_index { |product, index| %>
> 	<%= product.size %>
> <% } %>
>

I would use the do/end syntax in place of the { } since you are having
it span multiple lines, and
since embedded in html. It is somewhat ambiguous because hashes can be
created with {}.

<% products.each_width_index do |product,index| %>
   <%= product.size %>
<% end %>

I think 'end' in embedded in HTML also gives a better visual for way a
code block stops in ruby.

Zach
This topic is locked and can not be replied to.