Ruby For Rails Question

I am confused about the following method on page 415. Where did the b come from? What is the end.reverse?

def rank(list)   list.uniq.sort_by do |a|     list.select {|b| a == b }.size   end.reverse end

Hi --

The do block that begins "list.uniq ..." extracts unique values from list, then sorts them by the expression:

  list.select {|b| a == b }.size

This expression extracts sublists of list, where each item (b) matches the current value of a. The "size" method then tallies the number of elements in each sublist. In short, we are sorting by the number of times that each unique item appears in the initial list.

This gives us a list of instruments which is sorted by the number of compositions in which it appears. "reverse" then reverses the order of the sorted list. (I think :slight_smile:

-r

P.S. This is really more of a Ruby question than a Rails one;       you might want to post such to ruby-talk@ruby-lang.org.