Beware the Class.new dragons

When using this neat snippet from Dr Nic for testing a plugin I use internally, I came across the behavior described by Avdi. The proposed solution works great!

A single line modification to Dr Nic’s class ensures it works with class_inheritable_accessor from ActiveSupport.

def create_class(class_name, superclass, &block)
	klass = Class.new superclass
	klass.instance_eval &block
	Object.const_set class_name, klass
end

Post a Comment

Your email is never shared. Required fields are marked *

*
*