10

I have the following:

sets = DataSet.all.group_by{ |data| [data.project_id, "-", data.thread_id].join(" ") }

<% sets.each do |range, datas| %>
  <p><%= range %>:</p>
  <% datas.each do |data| %>
     <%=data%>
    <p>Last Post<%= data.last.created_at %></p>
  <% end %>
<% end %>

Problem is that I need an index. So I updated the above with:

<% sets.each_with_index do |range, datas, i| %>
  <p><%= range %>:</p>
  <% datas.each do |data| %>
     <%= i %>
     <%=data%>
    <p>Last Post<%= data.last.created_at %></p>
  <% end %>
<% end %>

That then breaks, with the error: undefined method `last' for 0:Fixnum

How can I fix this?

1 Answer 1

18

The issue you observe is because of the way parameters are assigned to the block. In your second example, you will observe that range contains an array containing a single range and the matching datas, the datas variable contains the index and i is always nil.

This is because ruby "unsplats" arrays if it is the only parameter to the block. If you have more than one type (in this case an array and an integer), you must hint ruby on what it should do. The simplest way is to use parentheses.

<% sets.each_with_index do |(range, datas), i| %>
  ...
<% end %>

That way, ruby will know what you mean and split the array up into range and datas. This is actually a feature of ruby's assignment operator in conjunction with the comma operator. It works like this

my_array = [1, 2]
(x, y) = my_array
p x                # prints 1
p y                # prints 2
1
  • Thanks Holger, first time I've hit this scenario. Commented Feb 12, 2011 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.