Skip to main content

Explain the complete_list vs incomplete_list blocks

  def sort_lists(lists, &block)
       complete_lists, incomplete_lists = lists.partition { |list| list_complete?(list) }

       incomplete_lists.each { |list| yield list, lists.index(list) }
       complete_lists.each { |list| yield list, lists.index(list) }
  end

<ul id="lists">
      <% sort_lists(@lists) do |list, index| %>
          <li class="<%= list_class(list) %>">
               <a href="/lists/<%= index %>">
                    <h2><%= list[:name] %></h2>
                    <p><%= todos_remaining_count(list) %> / <%= todos_count(list) %></p>
               </a>
          </li>
      <% end %>

</ul>

We want the list to populate incomplete_list first then completed_list second. So we pass in the partition method that will create one array for values that return true, and another array for values that return false. The list_complete? method will return true if the list is complete.

so we call each on incomplete_list first , then yield the list itself in the first block argument, and index in the second block argument. Then we call complete_list second, this order will create the impression that incomplete_list comes first.

In the HTML , sort_list will yield incomplete_list first and complete_list second.

Comments