Why does this include fail in instance_exec?

The following code will dynamically create a class, C, inside of module
A. Class C would like to include module B which is also defined in
module A but the ‘include B’ line for C fails with `module_eval’:
uninitialized constant Class::B (NameError).

I’ve included a class D definition as well which obviously works.

module A
end

def create_class(c, &b)
    klass = A.const_set(c.capitalize, Class.new)
    klass.instance_exec(&b)
end
A.module_eval <<-EOF
    module B
        def self.append_features(m)
            puts "B"
        end
    end
    class D
        include B
    end
    create_class :c do
        include B
    end
EOF

Any insight would be greatly appreciated.

Thanks,
Charlton

I found a post that address this. I guess the nature of
instance_exec/eval has changed with respect to bindings.

http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/

Charlton Wang wrote:

The following code will dynamically create a class, C, inside of module
A. Class C would like to include module B which is also defined in
module A but the ‘include B’ line for C fails with `module_eval’:
uninitialized constant Class::B (NameError).

I’ve included a class D definition as well which obviously works.

module A
end

def create_class(c, &b)
    klass = A.const_set(c.capitalize, Class.new)
    klass.instance_exec(&b)
end
A.module_eval <<-EOF
    module B
        def self.append_features(m)
            puts "B"
        end
    end
    class D
        include B
    end
    create_class :c do
        include B
    end
EOF

Any insight would be greatly appreciated.

Thanks,
Charlton