Creating Classes on the Fly

If you need to create a Ruby class during run time you can use Class.new. If you provide it with a class as an argument the given class becomes the superclass of the created class.

The Carrierwave gem uses this to create uploaders for file versions. Here is a simplified extract:

class Uploader
  def build_version(name, options)
    uploader = Class.new(self)
    # ...
  end
end

In the above, uploader will be a class whose parent class is Uploader.