On 2-okt-2007, at 1:53, Federico Brubacher wrote:

> I'm working in Rails but I'm sure this is a 100% Ruby question. I'm
> trying to use define_method for the first time, it takes the name  
> of the
> method and a block.

Oh sorry didn't read your case. So you want to select something  
presorted by X, right?
I know giving Rails-specific advice is not appreciated on this list  
but why don't do it in the published fashion then

module Sorts
   def method_missing(m, *a)
     if m.to_s =~ /^sort_by_/
       col = m.to_s.gsub(/^sort_by_/, '')
       find(:all, :order => "#{col} DESC")
     else
       super(m, *a)
     end
   end
end

class << ActiveRecord::Base
   include Sorts
end

 >> AnyActiveRecordDescendant.sort_by_id.map{|f| f.id }
=> [2, 1]

You can optionally extend that to handle things like  
"sort_by_foo_and_bar_and_baz" easily. And you certainly don't want  
human_name
because it's the one with spaces, y'know ;-)

define_method, class methods and blocks are tricky because of  
scoping, so I would do it that way.

BTW, i'm also curious how you should use define_method with class  
methods.

-- 
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl