From: Tony Mobily Date: 2006-06-26T10:27:35+09:00 Subject: Re: Help with "generating" code Hi, ***THANK YOU*** I ended up using a solution that was a little different to what I originally thought of. However, your input was *vital* for me. The main problem was that I was trying to write a class method, where an instance method was probably a better idea. Let me show what I did. I know this is RoR code. However, what I am focusing on is Ruby code here. So, here is what I did. In the application controller: class Lookup::AccountNumberController < ApplicationController [...] def create standard_create :account_number end [...] Then, in the ApplicationController the real magic happens: class ApplicationController < ActionController::Base [...] def standard_create(name) # Fetch the class's pointer called "Name" - THANK YOU Ruby ML! klass = self.class::const_get(Inflector.classify(name.to_s)) # Set @name. For example: # @channel = Channel.new(params[:channel]) - - THANK YOU Ruby ML! variable=instance_variable_set('@'+name.to_s,klass.new(params[name]) ) # Try and save the object, blah blah if variable.save flash[:notice] = "#{name.to_s.humanize} was successfully created." redirect_to :action => 'index' if ! parent else render :action => 'new' end end [...] end The big issue is that I needed to assign an instance variable, but all I had was the name. So, instance_variable_set('@'+name.to_s,klass.new(params[name]) ) really did the trick. Also, it took me a while to get klass = self.class::const_get(Inflector.classify(name.to_s)) (OK, it probably sounds totally obvious to you...). That variable=instance_variable_set('@'+name.to_s,klass.new(params[name]) ) is a bit of an eye sore. I wonder if there's a better way of "pointing" to the instance variable directly...? Anyway... Ruby truly is beautiful. In my application, I have *LOADS* and *LOADS* of tables that are ridicolously similar. Well, this techniques literally saves me from cutting&pasting the same code over and over again. I will probably end up writing an article about this (when I am more comfortable with what I'm doing). Bye! Merc. -- Posted via http://www.ruby-forum.com/.