"Mathieu Bouchard" <matju / sympatico.ca> wrote in message
news:Pine.LNX.3.96.1010516004053.16575I-100000 / relayer...
> On Wed, 16 May 2001, Hal E. Fulton wrote:
> > Hello all,
> > I was thinking about this today. I'd like to be able to define
> > some "ways of sorting" objects (i.e., by different fields)
> > and just plug them in.
> > Passing a block to "sort" is neat, but it's a little more
> > intrusive than I have in mind.
>
...
>
> class Array
> def sort_by(sym)
> self.sort {|x,y| x.send(sym) <=> y.send(sym) }
> end
> end
>
> or:
>
> class Array
> def sort_by(sym)
> self.sort(&eval %{
> proc {|x,y| x.#{sym} <=> y.#{sym} }
> })
> end
> end
>
> The latter should be faster.
> warning: Haven't tried either.
>
The latter doesn't work.

Why don't you try this?

class Array
  def sort_by(sym)
    eval("sort {|x,y| x.#{sym} <=> y.#{sym}}")
  end
end

>
> matju
>
>

Park Hee-Sob