I would like to know where put monkey patching code like the following in my rails application which would open up existing classes and add or override new functionality. I want this code to be available to all instances as soon as possible. Is autoload the correct way of doing this and putting the call in environment.rb?
class Class
def attr_initializer(*attributes)
attr_reader *attributes
class_eval <<-RUBY
def initialize(#{attributes.join(', ')})
#{attributes.map{ |attribute| "@#{attribute}" }.join(', ')} = #{attributes.join(', ')}
end
RUBY
end
end
If you are using rails 2.3.x then the standard place to put these is within a file in the config/initializers directory. Rails will load these files early in the boot process.