Why does this include fail in instance_exec?

C

Charlton Wang

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top