All,

I have five accessor methods in an AR descendant that are virtually the 
same.  I wanted to try and use define_method to auto-define all five 
methods, but I can't seem to get it to work.

Here are the methods:

  def gross_receipts=(new_value)
    write_attribute(:gross_receipts, new_value.to_s.gsub(/,/, ''))
  end

  def number_of_owners=(new_value)
    write_attribute(:number_of_owners, new_value.to_s.gsub(/,/, ''))
  end

  def annual_employee_payroll=(new_value)
    write_attribute(:annual_employee_payroll, new_value.to_s.gsub(/,/, 
''))
  end

  def uninsured_subcontractors_contract_cost=(new_value)
    write_attribute(:uninsured_subcontractors_contract_cost, 
new_value.to_s.gsub(/,/, ''))
  end

  def insured_subcontractors_contract_cost=(new_value)
    write_attribute(:insured_subcontractors_contract_cost, 
new_value.to_s.gsub(/,/, ''))
  end

and here is what I tried to do to auto-create them:

  for attr in [ :gross_receipts, :number_of_owners, 
:annual_employee_payroll, :uninsured_subcontractors_contract_cost, 
:insured_subcontractors_contract_cost ] do
    self.class_eval do
      define_method("#{attr.to_s}=(new_value)") {
        write_attribute(attr, new_value.to_s.gsub(/,/, ''))
      }
    end
  end

The for loop is syntactically correct, but these methods do not exist if 
I create a new instance of this object.

Is it because these methods don't exist in the first place (they're 
auto-created by ActiveRecord anyway)?

What am I doing wrong?

Thanks,
Wes

-- 
Posted via http://www.ruby-forum.com/.