On Feb 4, 2006, at 4:40 AM, Wybo Dekker wrote:

> When I have an array of objects (Thing's, say) with two properties  
> (say
> `name' and `amount') I want to be able to sort that array by either of
> these properties, depending on circumstances (like the value of the
> variable `order').

I would use send():

#!/usr/bin/ruby

class Thing
   attr_reader :name,:amount

   def initialize(name,amount)
     @name,@amount = name,amount
   end

   def list
     puts ['',@name,@amount].join("\t")
   end
end

arr = [
   Thing.new('John',10),
   Thing.new('Anny',20)
]

order = :name

puts "sorted by #{order}:"
arr.sort_by { |thing| thing.send(order) }.each { |a|
     a.list
     # much more code may occur here...
} rescue puts("illegal order")

__END__

Hope that helps.

James Edward Gray II