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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 20:42 UTC
Re: Ruby For Rails Question
Hi -- On Wed, 11 Oct 2006, Bala Paranj wrote:> > 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 > endb is the block param for the second block (the one that goes with select). The reverse reverses the results up to that point. Basically it''s a chain of array transformations. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
At 1:11 PM -0700 10/11/06, Bala Paranj wrote:> 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 > endThe 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 :-) -r P.S. This is really more of a Ruby question than a Rails one; you might want to post such to ruby-talk-X+L+6nJQZ5+NAS+ACkO+tw@public.gmane.org -- http://www.cfcl.com/rdm Rich Morin http://www.cfcl.com/rdm/resume rdm-go8te9J4rpw@public.gmane.org http://www.cfcl.com/rdm/weblog +1 650-873-7841 Technical editing and writing, programming, and web development --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
So reverse is not a method on end object? It can be end.any_method_that_is_available_in_the_return_object? b is the block param for the second block (the one that goes with select). The reverse reverses the results up to that point. Basically it''s a chain of array transformations. David --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-12 01:31 UTC
Re: Ruby For Rails Question
Hi -- On Wed, 11 Oct 2006, Bala Paranj wrote:> > So reverse is not a method on end object? It can be > end.any_method_that_is_available_in_the_return_object?Right. end is not an object; it signals the end of the code block. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
What''s the example data look like? Can someone share an end to end example? I''m curious. On 10/11/06, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > Hi -- > > On Wed, 11 Oct 2006, Bala Paranj wrote: > > > > > So reverse is not a method on end object? It can be > > end.any_method_that_is_available_in_the_return_object? > > Right. end is not an object; it signals the end of the code block. > > > David > > -- > David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
At 10:27 PM -0400 10/11/06, x1 wrote:> What''s the example data look like? Can someone share > an end to end example? I''m curious.Sure. -r % cat r4r_415 #!/usr/bin/env ruby # # r4r_415 - test code from p. 415 of "Ruby for Rails" def main puts rank( %w{a b a c i d a r i a n} ).join('', '') end def rank(list) list.uniq.sort_by do |a| list.select do |b| a == b end.size end.reverse end main % r4r_415 a, i, d, r, c, b, n -- http://www.cfcl.com/rdm Rich Morin http://www.cfcl.com/rdm/resume rdm-go8te9J4rpw@public.gmane.org http://www.cfcl.com/rdm/weblog +1 650-873-7841 Technical editing and writing, programming, and web development --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---