SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array index while looping

    Is there any way to get an objects array index as you loop through an array in Ruby?

    I'm working with the following code where @ta is set as the array index:
    Code:
    <% @ta=0 %>
    <% for article in @articles -%>
      <%= render_partial "article", article %>
      <% @ta+=1 %>
    <% end -%>
    This seems very primative to me and prone to errors. Is there anyway to do something like the following:
    Code:
    <% for article in @articles -%>
      <% @ta = article.index %>
      <%= render_partial "article", article %>
    <% end -%>
    I tried it but got an error.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I found it: each_with_index

    Have it working in a standalone script now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •