From: Calamitas Date: 2007-05-09T23:44:54+09:00 Subject: Re: Implementation of the object.sort method. On 5/9/07, Jorge Domenico Bucaran Romano wrote: > Hi, > > I want a demonstrative implementation of the sort method to see how the > callback (passed block) is handled, specifically the comparison result > <=>. > > For example, in the following code: > > puts [4,5,3,2,1].sort do |a,b| > -1 > end > > puts [4,5,3,2,1].sort do |a,b| > 1 > end > > Both print the array sorted down up. I don't understand how this is > possible so I wanted to see a demonstrative (but factual) implementation > of the sort method to see how this parameters are handled. The block binds to the call to puts and not to the call to sort, or in other words the above is equivalent to: puts([4,5,3,2,1].sort) do |a,b| -1 end You can use parentheses to disambiguate, or use curly braces: puts( [4,5,3,2,1].sort do -1 end ) puts [4,5,3,2,1].sort { -1 } puts( [4,5,3,2,1].sort { -1 } ) Peter