From: Matthew Bennett Date: 2009-07-12T01:21:50+09:00 Subject: Re: one line sorting Haris Bogdanovi� wrote: > Hi. > > I would like to implement a sorting algorithm in one line of code if > possible. > > n=[7,4,9,55,98,11,42] > n.inject(n[0]) {|min, i| min=i if i Do you actually need to implement the specific algorithm? [2,3,1].sort will sort it quite efficiently, or .sort! which will replace the array with the sorted version, and you can pass it a block to tell it what should be classed as "bigger", so that [2,3,1].sort { |a, b| b <=> a } would give equivalent results to [2,3,1].sort.reverse If you do need to implement that exact algorithm, my best guess is something along the lines of q = [7,4,9,55,98,11,42] a = []; until q.empty? ; a << q.delete(q.min) ; end There's probably a better way though (there always is!) Array#min is a method of Enumerable. > The result is a number object. Where do I (in general) put a dot in above > expression to perform some methods on that object (when expression has a > block {} of code in itself ) ? > You can put it at the end of the block, ruby's nice like that. So you could do: [2,3,1].sort { |a, b| a <=> b }.reverse Regards, Matthew PS: Haris pipped me to the post! Still, this might be useful anyway...