Using variable class names, how?


I've got some code, which I would like to reuse. The only difference between the usages are the class names and therefore the different objects. How would you manage this? Is there a way to take the class names as strings? Do you know other solutions, a suitable pattern, for example?

Thanx for any suggestions!

Best regards, ms

Start by reading this ->

The group archive is always a good place to start your search before sending messages.

Thanks for your answer, I read the thread, but this did not really help me. It's not just about redefining some core methods. I think, the description of my problem was not clear enough, sorry for this. I would appreciate, if you could have a look at the following explanation.

I've got several entities saved in my database: E1, E2 ... En. All these entities can be flexibly annotated, so I've got the following models:

E1 (Entity) => The entity itself EA1 (EntityAnnotation) => The various entity annotations, may be nested

Sorry. but i'm completely clueless. Can't really understand what you're trying to say/do.

Hey, no problem, thank you for taking time! :slight_smile:

-- ms

Hey, no problem, thank you for taking time! :slight_smile:

Is "Foo".constantize what you're looking for ?


Instead of passing class names as strings, why not ref the classes themselves? Lame example, but ... something like:

  ...   def first_last_count(clss)     return [clss.find(:first), clss.find(:last), clss.count]   end

  ...   first_car,last_car,count_cars = first_last_count(Car)   ...   first_bike,last_bike,count_bikes = first_last_count(Bike)   ...


Jeff Lewis wrote:

  def first_last_count(clss)

Note the tradition there is klass:

Being 'class' is a keyword...

My example used clss not class, ... but sure klass if you prefer.