From: gabriele renzi Date: 2005-12-07T02:47:34+09:00 Subject: Re: Good Ruby Examples? Wayne Vucenic ha scritto: > Hi Hampton, > > >>What I'm asking you guys for is if you have any short code ideas that >>can be programmed *really* quickly in Ruby that might illustrate its >>versatility. Some code example that would be hell to write in other >>languages that would be interesting for an hour long explaination. > > > My favorite is the well-known quicksort algorithm: > > def quicksort(a) > return a if a.size <= 1 > pivot = a[0] > quicksort(a.select {|i| i < pivot }) + > a.select {|i| i == pivot } + > quicksort(a.select {|i| i > pivot }) > end > > quicksort is "hell" to write in C or C++ unless you're extremely clever. you may appreciate Enumerable#partition: def qs(a) return a if a.size <=1 pivot = a.shift less, more = a.partition{|y| y < pivot} qs(less) + [pivot] + qs(more) end